C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Communities   Interview   Jobs   Projects   Offshore Development    
Silverlight Tutorials | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Revenue Sharing |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...

New Feature: Community Sites: Create your own .NET community website and start earning from Google AdSense ! It's Free !




Find the sum of cell values in a selected column in the DataGrid View as in Excel


Posted Date: 12 Mar 2007    Resource Type: Code Snippets    Category: DataGridView

Posted By: Balamurali Balaji       Member Level: Diamond
Rating:     Points: 10



The following code is used to finds the the sum of all the values in a selected column and display it in a textbox.

You may replace the 'Sum' function with Avg,Max, Min and other aggregate functions also.


Place the following code in the Form_Load Event handler. This would set the sortmode of the columns from Automatic to programmatic. It also sets the SelectionMode for the columns. You can click the column header to select a particular column.



foreach (DataGridViewColumn d in dataGridView1.Columns)
d.SortMode = DataGridViewColumnSortMode.Programmatic;

dataGridView1.SelectionMode = DataGridViewSelectionMode.ColumnHeaderSelect;



Place the following code in any button click event.



textBox1.Text = DataSet1.Tables[0].Compute("sum(" + dataGridView1.SelectedColumns[0].HeaderText + ")", "").ToString();







Responses

Author: Khurram Shahzad    26 Jul 2008Member Level: Silver   Points : 0
nice its a good idea of selected column
thanks


Feedbacks      
Popular Tags   What are tags ?   Search Tags  
(No tags found.)

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Change font for selected Cells in Datagridview control as in Excel
Previous Resource: Prevent multiple columns to be selected in a DatagridView.
Return to Discussion Resource Index
Post New Resource
Category: DataGridView


Post resources and earn money!
 
Related Resources



dotNet Slackers   BizTalk Adaptors    Web Design

conference calls

Contact Us    Privacy Policy    Terms Of Use