none
Delete a row from a databound DataGridView (C#) RRS feed

  • Question

  • I used the following lines of code to databind the DGV

    SqlConnection cnn = new SqlConnection(@"Data Source=.\SQLExpress;Initial Catalog=DBNAME;User Id=sa;Password=p@sswOrdWink;
    SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM SampleTable", cnn);
                SqlCommandBuilder cb = new SqlCommandBuilder(da);
               
    DataTable dt = new DataTable();
                da.Fill(dt);

                dataGridViewAccHead.DataSource = dt;

    To delete a row from the DGV i used the foll code in a button click event:

      if (dataGridViewAccHead.SelectedRows.Count > 0)
                {
                    dt.Rows[dataGridViewAccHead.SelectedRows[0].Index].Delete();
                }

    I'm able delete rows... But i had a problem..

    Suppose if I selected 2nd row of DGV and deleted it,i'm not able to delete the 2nd row that is displayed in the DGV after the first deletion.

    Similarly thing happens for any other row too..

    Whether my code for deleting the databound row of DGV is right?

    If not suggest me the right one...
    Thursday, June 19, 2008 10:25 AM

Answers

  • The above code didn't work for me.

    I couldn't identify the problem ... But the following line of code to delete worked fine.

      if (dataGridViewAccHead.SelectedRows.Count > 0)
                {
                    dt.Rows.Find(dataGridViewAccHead.SelectedRows[0].Cells[2].Value.ToString()).Delete() ;
                }

    For the above code to work, Primary key should be set in the DataTable. To do that the following line of code can be used to set the Primary Key in the database to the DataTable.. Use it before u fill the DataSet using the TableAdapter

      da.MissingSchemaAction = MissingSchemaAction.AddWithKey;
    Friday, June 20, 2008 7:22 AM

All replies

  • Code Snippet
      if (dataGridViewAccHead.SelectedRows.Count > 0)

                {
                    dt.Rows[dataGridViewAccHead.SelectedRows[0].Index].Delete();
                }

                dataGridViewAccHead.DataSource = dt;

     

     

     

    Friday, June 20, 2008 2:18 AM
  • The above code didn't work for me.

    I couldn't identify the problem ... But the following line of code to delete worked fine.

      if (dataGridViewAccHead.SelectedRows.Count > 0)
                {
                    dt.Rows.Find(dataGridViewAccHead.SelectedRows[0].Cells[2].Value.ToString()).Delete() ;
                }

    For the above code to work, Primary key should be set in the DataTable. To do that the following line of code can be used to set the Primary Key in the database to the DataTable.. Use it before u fill the DataSet using the TableAdapter

      da.MissingSchemaAction = MissingSchemaAction.AddWithKey;
    Friday, June 20, 2008 7:22 AM
  • hi,

    i have faced the same problem, if ur data grid view is bounded to data source then i advice you to do so:

    note: my data source is a List<KeyValuePair<stringstring>>

     

    privatevoid deleterow_lbl_Click(object sender, EventArgs e)         {             int i = this.datagrid.SelectedCells[0].RowIndex;             if (toboundlist.Count >= i)             {

    toboundlist.RemoveAt(i);

    }

                this.datagrid.DataSource = null;
                this.datagrid.DataSource = toboundlist;
            }

    Thursday, September 27, 2012 10:51 AM