E
E
EgorP132018-03-11 15:10:35
Database
EgorP13, 2018-03-11 15:10:35

How to sum cell value from selected columns of DataGridView in Label?

Hello, I'm trying to transfer the selected data from the cell to the label, when selecting a lot of cells from different rows, they do not add up, and the label is simply assigned the value of the last selected row. I don’t know how to implement the addition of different cells in the rows. Tell me what you can do please.
Tried to do like this:

foreach (DataGridViewRow row in молоко_и_молочные_продуктыDataGridView.SelectedRows)
            {
 
 
                d += Convert.ToDouble(row.Cells[1].Value.ToString());
               
                label19.Text = d.ToString();
            }

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
ViaCom, 2018-03-11
@ViaCom

Well, if the topic is in TSQL, then the answer is in it

declare @s nvarchar(max) = ''
select @s = @s + Your_String_Column from Your_Table
select @s

This can be wrapped in a function or procedure ... as you like

I
Ivan Arxont, 2018-03-11
@arxont

Try like this

foreach (DataGridViewRow row in молоко_и_молочные_продуктыDataGridView.SelectedRows)
{
   double number;
   string value =  row.Cells[1].Value.ToString();

   if (Double.TryParse(value, out number))
      d += number;
}

label19.Text = d.ToString();

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question