Click here to Skip to main content
15,881,715 members
Please Sign up or sign in to vote.
3.67/5 (2 votes)
See more:
My DataGridView is bound by DataTable (csvData). Since the grid mirrors the table, I'm thinking . . . this.DataGridView1.SelectedRows.Count would be the same row of the table. But how do I assign that selected row to the row (DataRow) of the table so I can delete it?
private void DeleteRowButton_Click(object sender, EventArgs e)
        {

            if (this.DataGridView1.SelectedRows.Count > 0 &&
           this.DataGridView1.SelectedRows[0].Index !=
           this.DataGridView1.Rows.Count - 1)

            {

              //  DataRowCollection rowCollection = csvData.Rows.Count();  ???
              //rowCollection.Remove();  ???? no help

                DataRow row;
                row.Delete();  // row not assigned ?

                
             }
          }
Posted
Updated 14-Jan-14 2:16am
v2
Comments
njammy 14-Jan-14 11:22am    
Debug and look at the Datasource, to see its structure, and properties.
You may need to get rows by indexed reference.

Assuming your DataSource is accessible inside that event, and assuming the index in your code really points to the index of the DataRow on your DataSource, you can try doing the following:

C#
int index = this.DataGridView1.SelectedRows[0].Index;
yourDataSource.Rows[index].Delete();
yourDataSource.AcceptChanges();
 
Share this answer
 
Comments
JOHNNYDEMICHAEL 16-Jan-14 20:28pm    
That's what I needed . . . THANKS!
walterhevedeich 16-Jan-14 20:33pm    
Glad it helped. You're welcome.
Try this..



C#
private void button1_Click(object sender, EventArgs e)
        {
           // foreach (DataGridViewRow row in dataGridView1.SelectedRows) // for row selection
            foreach (DataGridViewCell cell in dataGridView1.SelectedCells)
            {
                var rowIndex = cell.RowIndex;
                dataTable.Rows[rowIndex].Delete();
                dataTable.AcceptChanges();

            }
        }</pre>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900