We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

Excel like Sum of Selected Range of Cells in GridControl

Hi,

I have a requirement in which i need to display the sum of selected range of cells from a GridControl to a label. Could you suggest me a best possible way to do this?

Thanks and Regards,
Sridhar N


4 Replies

AD Administrator Syncfusion Team August 18, 2008 12:36 PM UTC

One way you can do this is to use the grid.SelectionChanged event.

void gridControl1_SelectionChanged(object sender, Syncfusion.Windows.Forms.Grid.GridSelectionChangedEventArgs e)
{
if (!e.Range.IsEmpty)
{
double sum = 0;
double d;
GridRangeInfo range = e.Range.ExpandRange(1, 1, gridControl1.RowCount, gridControl1.ColCount);
int row, col;
if (range.GetFirstCell(out row, out col))
{
if (gridControl1[row, col].CellValue != null && double.TryParse(gridControl1[row, col].Text, out d))
sum += d;
while (range.GetNextCell(ref row, ref col) && gridControl1[row, col].CellValue != null
&& double.TryParse(gridControl1[row, col].Text, out d))
{
sum += d;
}
this.label1.Text = string.Format("{0}", sum);
}
}
else
this.label1.Text = "";
}




SR Sridhar August 21, 2008 09:14 AM UTC

Thanks a lot.

>One way you can do this is to use the grid.SelectionChanged event.

void gridControl1_SelectionChanged(object sender, Syncfusion.Windows.Forms.Grid.GridSelectionChangedEventArgs e)
{
if (!e.Range.IsEmpty)
{
double sum = 0;
double d;
GridRangeInfo range = e.Range.ExpandRange(1, 1, gridControl1.RowCount, gridControl1.ColCount);
int row, col;
if (range.GetFirstCell(out row, out col))
{
if (gridControl1[row, col].CellValue != null && double.TryParse(gridControl1[row, col].Text, out d))
sum += d;
while (range.GetNextCell(ref row, ref col) && gridControl1[row, col].CellValue != null
&& double.TryParse(gridControl1[row, col].Text, out d))
{
sum += d;
}
this.label1.Text = string.Format("{0}", sum);
}
}
else
this.label1.Text = "";
}






SR SubhaSheela R Syncfusion Team August 25, 2008 04:11 AM UTC


Hi Sridhar,

Thanks for your update.

Please feel free to open new one if you have any other concern.

Regards,
Subhasheela R



GP Gaurav Pandey February 10, 2012 03:54 PM UTC

Well, this technique just works if I select a contiguous set of cells next to each other. If I select few cells which are not in a sequence, how will I be able to retrieve those? I need to do the same thing as the original post - compute the sum of the selected cells in the grid control with an additional requirement - selection is not done in a sequence.

Thanks,
Gaurav


Loader.
Live Chat Icon For mobile
Up arrow icon