How can I total a datagrid column. I want to display the total in the status bar.
If I knew how to get the -total number of rows-, (not visiblerows), I think I can just loop through and add up each columns value to get the total.
CB
Clay Burch
Syncfusion Team
April 23, 2002 09:37 AM UTC
One way to get at the total number of rows in a datagrid is through its DataSource member. Exactly how you go about this depends upon the exact type of the DataSource. For example, if it is a DataTable, then you can get the number of rows by getting the count of the row collection from the datatable. Here is a code snippet suggesting how.
//if Datasource is DataTable,br>
dataGrid1.DataSource = _dataSet.Tables["customers"];,br>
// then try this...
int numRows = _dataSet.Tables["customers"].Rows.Count;
CS
CSHANNON
April 23, 2002 07:48 PM UTC
> One way to get at the total number of rows in a datagrid is through its DataSource member. Exactly how you go about this depends upon the exact type of the DataSource. For example, if it is a DataTable, then you can get the number of rows by getting the count of the row collection from the datatable. Here is a code snippet suggesting how.
>
>
> //if Datasource is DataTable,br>
> dataGrid1.DataSource = _dataSet.Tables["customers"];,br>
>
>
> // then try this...
> int numRows = _dataSet.Tables["customers"].Rows.Count;
CS
CSHANNON
April 23, 2002 07:53 PM UTC
I should have given more details. What I am doing is: On the left of the form is a listbox which holds the master table info i.e. Customers.
To the right is a datagrid which uses the Orders table. A relationship is established between the tables so that in the datagird, you only see orders for the customer selected in the listbox.
I want to show a total on the form (for all orders for the selected customer). When you select another customer, the total should change to show the new total for the different customer.
CB
Clay Burch
Syncfusion Team
April 24, 2002 09:35 AM UTC
If you are using a DataRelation to define your details grid, I don't know of a good way to get at the detail grid row count.
But you could use a DataView with a filter to bind to the detail grid. To do so, you could handle the master grids CurrentCellChanged event, and if the row is changing, rebind a new DataView with the proper filter to the detail grid. Then in this case, you could easily get at the row count in the detail grid through this DataView object.
CB
Clay Burch
Syncfusion Team
April 24, 2002 03:02 PM UTC
If you are using a DataRelation to define the Master-Details grid, then here is how you can get the number of rows in the details. You do it through the BindingContext member of the Details grid.
label1.Text = dataGridDetails.BindingContext[dataGridDetails.DataSource,
dataGridDetails.DataMember].Count.ToString();