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.
Unfortunately, activation email could not send to your email. Please try again.

Excel like Sum of Selected Range of Cells in GridControl

Thread ID:

Created:

Updated:

Platform:

Replies:

75947 Aug 18,2008 03:25 AM Feb 10,2012 10:54 AM Windows Forms 4
loading
Tags: GridControl
Sridhar
Asked On August 18, 2008 03:25 AM

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


Administrator [Syncfusion]
Replied On August 18, 2008 08:36 AM

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 = "";
}



Sridhar
Replied On August 21, 2008 05:14 AM

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 = "";
}





SubhaSheela R [Syncfusion]
Replied On August 25, 2008 12:11 AM


Hi Sridhar,

Thanks for your update.

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

Regards,
Subhasheela R


Gaurav Pandey
Replied On February 10, 2012 10:54 AM

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


CONFIRMATION

This post will be permanently deleted. Are you sure you want to continue?

Sorry, An error occured while processing your request. Please try again later.

You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.

;