I am struggling with the problem of summing filtered rows of datagridview. I can sum the whole datatable with the following program. But I want to summarize only filtered rows of datatable. How can I do that?
Code:
private void sumRows(DataTable table)
{
DataRow row = table.NewRow();
for (int i = 1; i < table.Columns.Count; i++)
{
row[i] = 0;
}
table.Rows.InsertAt(row, 0);
Decimal num, sum;
for (int i = 1; i < table.Columns.Count; i++)
{
sum = 0;
foreach (DataRow r in table.Rows)
{
try
{
if (Decimal.TryParse(r[i].ToString(), out num))
{
sum += num;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
row[i] = sum.ToString();
}
}
Comment