How to update the data source after updating the data grid view in c#

  Uncategorised

I am adding a delete function in my data grid view at the moment.
I add the delete button by code, and I plan to trigger the delete function via Event.

private void DataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
    {
        //if click is on new row or header row
        if (e.RowIndex == dataGridView1.NewRowIndex || e.RowIndex < 0)
            return;

        //Check if click is on specific column 
        if (e.ColumnIndex == dataGridView1.Columns["dataGridViewDeleteButton"].Index)
        {
            
            System.Console.WriteLine("delete button pressed!");
            dataGridView1.Rows.RemoveAt(e.RowIndex);

            
        }
    }

I have been tested the function, the debug console actually prints out the delete button pressed!, which means the Event is triggered successfully.
However, I am running into an issue; how to update the underline data source after I removed the cell at specific row?
The data source code is :

DataTable dataTable = new DataTable();
// data loading ...

Source: Visual Studio Questions

LEAVE A COMMENT