DataGridView capturing user row selection

I am having trouble handling the selections in DataGridView. My grid view contains an amount column. There is a textbox on the form which should display the total amount of the selected grid view rows. Hence I need to capture events when the user selects/ deselects the gridview rows and calculate (add/ subtract) the amount accordingly. I have found two methods of doing it:

  1. Using the RowEnter and RowLeave events. These work fine when user selects/ deselects a single row. However, when the user is selecting multiple rows at one go, the event gets fired only for the last row. Hence, from my total amount only the amount in the last row gets added/ subtracted. Thus making my result erroneous.

  2. Using the RowStateChanged event. This works for multiple rows. However, the event gets fired event if the user scrolls through the datagrid.

Has anyone handled such a scenario. I would like to know which datagrid event I should be using, so that my code executes only when user selects/ deselects rows including multiple rows.


Found the solution. I can use RowStateChanged and run my code only if StateChanged for the row is Selected...

private void dgridv_RowStateChanged(object sender, DataGridViewRowStateChangedEventArgs e)
    // For any other operation except, StateChanged, do nothing
    if (e.StateChanged != DataGridViewElementStates.Selected) return;

    // Calculate amount code goes here

I use SelectionChanged event or CellValueChanged event:

        dtGrid.SelectionChanged += DataGridView_SelectionChanged;
        this.dtGrid.DataSource = GetListOfEntities;
        dtGrid.CellValueChanged += DataGridView_CellValueChanged;

    private void DataGridView_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        DataGridViewRow row = dtGrid.Rows[e.RowIndex];

    private void DataGridView_SelectionChanged(object sender, EventArgs e)
        var rowsCount = dtGrid.SelectedRows.Count;
        if (rowsCount == 0 || rowsCount > 1) return;

        var row = dtGrid.SelectedRows[0];
        if (row == null) return; 

I think you can consider the SelectionChanged event:

private void DataGridView1_SelectionChanged(object sender, EventArgs e) {
  textbox1.Text = DataGridView1.SelectedRows.Count.ToString();

You can simply capture this in following way, but it is restricted to single row selection only:

private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
    MessageBox.Show("Selected row is=" + e.RowIndex);
    //  you can perform (any operation) delete action on selected row like


You can use your first method (row enter row leave) along with the SelectedRows property. Meaning, when you detect those events, and you need to calculate, instead of using the row from the event args, loop through the SelectedRows and get your total.

You can use SelectionChanged event and retrieve the row index of the selected cell(s):

    private void dataGridView1_SelectionChanged(object sender, EventArgs e)
        if (dataGridView1.SelectedCells.Count > 0)

Need Your Help

Mongodb background indexes - are they still background once created?

mongodb indexing mongodb-indexes

When creating an index in mongodb, you can specify the background: true flag, which causes the index creation to be non-blocking. This is great in production since you don't want the whole database

Laravel Eloquent Join vs Inner Join?

php mysql sql laravel eloquent

So I am having some trouble figuring out how to do a feed style mysql call, and I don't know if its an eloquent issue or a mysql issue. I am sure it is possible in both and I am just in need of som...