Show total of datagridview selected rows in toolstrip label c#

ravi kumar 331 Reputation points
2021-01-20T11:32:55.96+00:00

In my win form application i need to show the sum of my datagridview selected rows in toolstrip label, i am currently able to calculate the sum and display in the label for all the rows in my data grid view. But my requirement is :

If the datagridview gets filtered the label should show only the total of that filtered rows.
If the user selects some rows in the whole datagridview the should show only the total of that selected rows.
please help me how to do this here is my code so far:

int A = 0, B = 0;
                for (A = 0; A < iP_SpoolsDataGridView.Rows.Count; ++A)
                {
                    B += Convert.ToInt32(iP_SpoolsDataGridView.Rows[A].Cells[20].Value);
                }
                Total.Text = B.ToString();

image of my winform:
mtf1y.png

Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,908 questions
Visual Studio
Visual Studio
A family of Microsoft suites of integrated development tools for building applications for Windows, the web and mobile devices.
5,267 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,121 questions
{count} votes

Accepted answer
  1. Daniel Zhang-MSFT 9,631 Reputation points
    2021-01-21T08:03:56.94+00:00

    Hi ravikumar-1532,
    First, you can use BindingSource.Filter property to filter the rows with dateTimePicker's value.
    Then you can use DataGridView.SelectedRows property to get the collection of rows selected.
    Meanwhile, you need to run these in DataGridView.SelectionChanged event which occurs when the current selection changes.
    Here is my test code you can refer to.(In my test, I calculate the sum of column Field2)

    public Form1()  
    {  
        InitializeComponent();  
        dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;  
    }  
      
    private void button1_Click(object sender, EventArgs e)  
    {  
        BindingSource bs = new BindingSource();  
      
        bs.DataSource = dataGridView1.DataSource;  
        bs.Filter = string.Format("Field2 >= #{0:MM/dd/yyyy}# And Field2 <= #{1:MM/dd/yyyy}#", dateTimePicker1.Value, dateTimePicker2.Value);  
      
    }  
      
    private void dataGridView1_SelectionChanged(object sender, EventArgs e)  
    {  
        int total = dataGridView1.SelectedRows.Cast<DataGridViewRow>().Sum(row => (int)row.Cells[1].Value);  
        label1.Text = total.ToString();  
    }  
    

    Test result:
    59028-121.gif
    Best Regards,
    Daniel Zhang


    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.