புள்ளிவலை

மூளைக்குத் தீனியில்லாத போழ்து
சிறிது மனதுக்கும் ஈயப்படும்.
posts - 23, comments - 60, trackbacks - 0

எண்கள் அடிப்படையிலான columnகளின் கூட்டுத்தொகையை க்ரிட்வியூவில் காண்பிக்க...

க்ரிட்வ்யூ கன்ட்ரோலை ஒரு டேட்டா டேபிள்/டேட்டாசெட்டுடன் பைன்ட் பண்ணிய பிறகு, எண்கள் இருக்கும் columnகளின் (numeric datatype columns) அடியில் மட்டும் எண்களின் கூட்டுத்தொகையைக் காண்பிக்க வேன்டியிருக்கலாம். பைன்ட் பண்ணிய டேபிளைப் பற்றிய விவரங்கள் நமக்கு முன்னமேயே தெரிந்திருந்தால் datatable.Compute("sum(columname)") மெத்தடைப் பயன்படுத்தி சுலபமாக இதைச் செய்துவிடலாம். ஆனால் இதற்கு எவையெவையெல்லாம் எண்களின் அடிப்படையில் அமைந்த fields என்று நிரல் எழுதும்போதே தெரிந்திருக்கவேண்டும். அப்படி இயலாத நேரங்களில், எந்தெந்த fields எண் வகையைச் சார்ந்தது என்று இயங்குநேரத்தில் கண்டுபிடித்து பிறகு Compute மெத்தடைப் பயன்படுத்தி sumஐ காண்பிக்கவேண்டும்.

ஒரு க்ரிட்வ்யூவையும், டேட்டாடேபிளையும் ஆர்க்யுமென்டாகக் கொடுத்தால், தானாகவே இந்த வேலையைச் செய்யும் ஒரு சின்ன ஃபங்க்ஷன் இதோ:

    private void GenerateFooterTotal(GridView gvResult, DataTable dtbl)
    {
        #region Total for number columns
        //We cant compute the total by specifying the field name since we dont
        //know what table will be displayed and what columns need to be included for the sum.
        //So, just get the sum of all the decimal and int type columns
 
        Hashtable hsh = new Hashtable(); //This will hold the columns and position of the columns that are of numeric type
 
        //Loop through each column to find out the numeric column.
        //If numeric column is found, then include it for computing the sum
        for (int intColPosition = 0; intColPosition < dtbl.Columns.Count; intColPosition++)
        {
            if (dtbl.Columns[intColPosition].DataType == typeof(decimal) || dtbl.Columns[intColPosition].DataType == typeof(int))
            {
                hsh.Add(dtbl.Columns[intColPosition], intColPosition);
            }
        }
 
        //Now loop through the hashtable holding the numeric type columns and compute the sum to display in the footer
        foreach (object obj in hsh.Keys)
        {
            //display the result (computed on the right side as assignment) in the footer = the sum of the particular numeric column name
            gvResult.FooterRow.Cells[Convert.ToInt32(hsh[obj].ToString())].Text = String.Format("{0:0.00}", dtbl.Compute("sum(" + ((DataColumn)obj).ColumnName + ")", "")).ToString();
            gvResult.FooterRow.Cells[Convert.ToInt32(hsh[obj].ToString())].Attributes["align"] = "right";
        }
        #endregion
    }
 
    private void FormatGridNumbers(GridView gvResult, DataTable dtbl)
    {
        System.Double dblValue = 0;
        foreach (GridViewRow grow in gvResult.Rows)
        {
            for (int intCellPosition = 0; intCellPosition < grow.Cells.Count; intCellPosition++)
            {
                if(double.TryParse(grow.Cells[intCellPosition].Text, out dblValue))
                {
                    grow.Cells[intCellPosition].Text = String.Format("{0:0.00;(0);0}", dblValue);
                }
            }
        }

    } 

Print | posted on Thursday, March 27, 2008 12:41 PM | Filed Under [ C# ஏஎஸ்பி.நெட் ]

Feedback

No comments posted yet.

Post Comment

Title  
Name  
Email
Url
Comment   
Please add 8 and 3 and type the answer here:

Powered by: