Results 1 to 11 of 11

Thread: Deleting a row from a DataGridView???

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Jan 2004
    Location
    Southern California
    Posts
    5,034

    Deleting a row from a DataGridView???

    I have a datagridview that I am trying remove a row. The grid is bound to a datatable and I'm deleting the row from the datatable...problem is I'm not sure how to reference the row in the grid. Here's my statement:

    dtTable.Rows.Remove(grd.CurrentRow.?)

    The ? is...what's next?

    Thanks,
    Blake

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Deleting a row from a DataGridView???

    That is not necessary. If the grid is bound to the DataTable then deleting the row from the DataTable will automatically remove it from the grid. That's the whole point of data binding: changes to the data source will be reflected in the bound control and vice versa.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    PowerPoster
    Join Date
    Jan 2004
    Location
    Southern California
    Posts
    5,034

    Re: Deleting a row from a DataGridView???

    I know...but I'm not sure how to reference the current row of the grid which is just a pointer the the Dataset row.
    Blake

  4. #4

    Thread Starter
    PowerPoster
    Join Date
    Jan 2004
    Location
    Southern California
    Posts
    5,034

    Re: Deleting a row from a DataGridView???

    Can anyone tell me how to delete a selected row from a bound DataGridView?

    I have a datatable that is bound to a grid. I just want to remove a row from the datatable.

    Thanks,
    Blake

  5. #5
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: Deleting a row from a DataGridView???

    As JMC said, "That's the whole point of data binding: changes to the data source will be reflected in the bound control and vice versa."
    So you can either remove rows form the datagridview or from the bound datatable itself..
    To remove selected rows from the datagridview:
    VB Code:
    1. For i As Integer = DataGridView1.SelectedRows.Count - 1 To 0 Step -1
    2.             DataGridView1.Rows.Remove(DataGridView1.SelectedRows(i))
    3.         Next
    And to remove selected rows from the table directly
    VB Code:
    1. Dim row As DataGridViewRow
    2.         For i As Integer = DataGridView1.SelectedRows.Count - 1 To 0 Step -1
    3.             row = DataGridView1.SelectedRows(i)
    4.             dt.Rows.RemoveAt(row.Index)
    5.         Next
    The trick is looping backward on the datagridview's selectedrows collection

  6. #6

    Thread Starter
    PowerPoster
    Join Date
    Jan 2004
    Location
    Southern California
    Posts
    5,034

    Re: Deleting a row from a DataGridView???

    Stan,

    I'm using the code to delete from the Datatable but it doesn't seem to be working. I'm selecting only 1 row from my datagrid and it's Row 0. It goes thru the loop fine but it doesn't remove the row from the grid. Do I need to refresh my grid after the statement?
    Blake

  7. #7
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: Deleting a row from a DataGridView???

    No, you don't have to refresh the datagridview at all... My question is, did you select a row entirely (by clicking on its row header) or did you just select a cell in a row?

  8. #8
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: Deleting a row from a DataGridView???

    also make sure the AllowUserToDeleteRows property is set to true

  9. #9

    Thread Starter
    PowerPoster
    Join Date
    Jan 2004
    Location
    Southern California
    Posts
    5,034

    Re: Deleting a row from a DataGridView???

    Kleinma,

    That was something that I didn't have set, however, it's still not deleting from the grid. I checked the datatable count and it shows that the row was removed but it's still not removing it from the bound datagridview. Is it different when you remove it from a datatable as opposed to a dataset? If that's the case then I'm not deleting it from the dataset. Here is the code that is supposed to remove it from the grid.
    VB Code:
    1. Dim row As DataGridViewRow
    2.  
    3.                 For i As Integer = grd.SelectedRows.Count - 1 To 0 Step -1
    4.                     row = grd.SelectedRows(i)
    5.                     dtConsignees.Rows.RemoveAt(row.Index)
    6.                 Next
    Thanks,
    Blake

  10. #10
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Deleting a row from a DataGridView???

    Removing a row and deleting the record are NOT the same thing:
    VB Code:
    1. DirectCast(myDataGridView.CurrentRow.DataBoundItem, DataRowView).Delete()
    or for multiple selected rows this should work:
    VB Code:
    1. For i As Integer = myDataGridView.SelectedRows.Count - 1 To 0 Step -1
    2.     DirectCast(myDataGridView.SelectedRows(i).DataBoundItem, DataRowView).Delete()
    3. Next i
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  11. #11

    Thread Starter
    PowerPoster
    Join Date
    Jan 2004
    Location
    Southern California
    Posts
    5,034

    Re: Deleting a row from a DataGridView???

    That worked JM...thanks!
    Last edited by blakemckenna; Jan 29th, 2007 at 04:13 PM.
    Blake

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width