Results 1 to 8 of 8

Thread: Deleting a row from the data source (datatable) of a datagridview

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2018
    Posts
    514

    Deleting a row from the data source (datatable) of a datagridview

    Hi,
    I am struggling to delete a row from a datatable that I have bound to my DGV (datagridview).
    The DGV is not full row selection thing and users can click only on one cell at time.
    I can delete the row from the DGV but the row is not actually deleted form the datatable.

    Will someone please help me to figure it out?

    Thanks.
    here my tires:


    Code:
          
    DataGridView1.Rows.RemoveAt(DataGridView1.CurrentCell.RowIndex)' Obviously nothing is happening to the datatabel here
    ''
    This is how I bind the datatable to the DGV:

    Code:
       Dim MyBindingSource As New BindingSource()
            If Shared_AlertTable_Local.Rows.Count > 0 Then
                Dim tempDT As DataTable = Shared_AlertTable_Local.DefaultView.ToTable(True, "Interval", "ToBeNotified", "Escalation", "WhomToEscalate", "HowToAlert")
    
                MyBindingSource.DataSource = tempDT
                With DataGridView1
                    .DataSource = MyBindingSource
                    .RowHeadersVisible = False
                    .EnableHeadersVisualStyles = False
                    .AllowUserToAddRows = False
                    .BackgroundColor = Color.White
                    .ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing
                End With
                EnableDoubleBuffered(DataGridView1)
            Else
                Call RemoveBindingSourceForDGV(DataGridView1)
            End If

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

    Re: Deleting a row from the data source (datatable) of a datagridview

    You should not be touching the grid in code. The BindingSource is supposed to be the one and only place that you access the bound data. When the user selects a row in the grid, you delete that row by calling RemoveCurrent on the BindingSource. That will delete the row in the data source and so it will no longer be displayed in the grid.

    BTW, don't create the BindingSource in code. Add it to the form in the designer, just as you do with the grid. Doing that means that you have a member variable with which to access the BindingSource, instead of having to get it from the DataSource of the grid and casting.

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2018
    Posts
    514

    Re: Deleting a row from the data source (datatable) of a datagridview

    Thank you for the explanation.
    I don't have a data source at design time; I will have a datatable that is created during the runtime. can that still be managed at design mode?
    Edit:
    RemoveCurrent removes the row from the datagidview but I checked and the row is still in the datatable with this code:

    Code:
    Dim MyBindingSource As New BindingSource()
            MyBindingSource = DataGridView1.DataSource
            MyBindingSource.RemoveCurrent()

    How will I delete the row in the table too then?
    Last edited by Grand; Aug 17th, 2021 at 06:19 AM.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Deleting a row from the data source (datatable) of a datagridview

    Quote Originally Posted by Grand View Post
    I don't have a data source at design time; I will have a datatable that is created during the runtime. can that still be managed at design mode?
    So you're asking whether you can manage something at design time that doesn't exist at design time? Do I really need to answer that?
    Quote Originally Posted by Grand View Post
    RemoveCurrent removes the row from the datagidview but I checked and the row is still in the datatable with this code:

    Code:
    Dim MyBindingSource As New BindingSource()
            MyBindingSource = DataGridView1.DataSource
            MyBindingSource.RemoveCurrent()

    How will I delete the row in the table too then?
    The row is deleted in the DataTable. That's how DataTables work. They are primarily intended to be used with a database. You make changes to the data in the DataTable and the DataRows will have their RowState set to Added, Modified or Deleted as appropriate. When you call Update on a data adapter and pass the DataTable as an argument, the Added, Modified and Deleted DataRows will be inserted, updated and deleted in the database accordingly. AcceptChanges is then implicitly called on the DataTable and all RowStates are set to Unchanged for Added and Modified rows and Deleted rows are removed.

    The question is, how exactly are you using your DataTable? Are you saving changes to a database? If not then how exactly are you using it? Maybe removing the row immediately is the right way to go or may simply deleting the row is enough as it is.

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2018
    Posts
    514

    Re: Deleting a row from the data source (datatable) of a datagridview

    No answer is needed, it was just my "better ask to be sure" mode.
    For the datatable, I am creating it at form load add then necessary columns to it. Then add rows to it when users select some options, show the result in the datagirdview.

    Code:
            With Shared_AlertTable_Local
                With .Columns
                    .Add("SubmittDate", GetType(String))
                    .Add("SubmittedBy", GetType(String))
                    .Add("SubmittedByDepartment", GetType(String))
                    .Add("Site", GetType(String))
                    .Add("Area", GetType(String))
                    .Add("ActiveStatus", GetType(String))
                    .Add("Interval", GetType(String))
                    .Add("ToBeNotified", GetType(String))
                    .Add("Escalation", GetType(String))
                    .Add("WhomToEscalate", GetType(String))
                    .Add("HowToAlert", GetType(String))
                End With
            End With
    ' button click to add the stuff to the datatable and show some selected columns in the datagirdview
     Private Sub Button115_Click(sender As Object, e As EventArgs) Handles Button115.Click
    
            Dim MyNewRow As DataRow = Shared_AlertTable_Local.NewRow
    
            Using WC As New WaitCursor
                With Shared_AlertTable_Local
                    With .Columns
                        MyNewRow("SubmittDate") = CurrentDatetime
                        MyNewRow("SubmittedBy") = LoggedInits
                        MyNewRow("SubmittedByDepartment") = TeamForLoggedInit
                        MyNewRow("Site") = MySite
                        MyNewRow("Area") = MyLocalArea
                        MyNewRow("ActiveStatus") = "Yes"
                        MyNewRow("Interval") = NumericUpDown1.Value
    
                        If Panel3.Visible = True Then
                            MyNewRow("ToBeNotified") = ComboBox7.SelectedItem.ToString
                        Else
                            MyNewRow("ToBeNotified") = ComboBox3.SelectedItem.ToString
                        End If
    
                        MyNewRow("Escalation") = ComboBox5.SelectedItem.ToString
    
                        If Panel2.Visible = True Then
                            MyNewRow("WhomToEscalate") = ComboBox6.SelectedItem.ToString
                        Else
                            MyNewRow("WhomToEscalate") = "NA"
                        End If
                        MyNewRow("HowToAlert") = ComboBox4.SelectedItem.ToString
    
                    End With
                    .Rows.Add(MyNewRow)
                End With
            End Using
    
            Dim MyBindingSource As New BindingSource()
            If Shared_AlertTable_Local.Rows.Count > 0 Then
                Dim tempDT As DataTable = Shared_AlertTable_Local.DefaultView.ToTable(True, "Interval", "ToBeNotified", "Escalation", "WhomToEscalate", "HowToAlert")
    
                MyBindingSource.DataSource = tempDT
                With DataGridView1
                    .DataSource = MyBindingSource
                    .RowHeadersVisible = False
                    .EnableHeadersVisualStyles = False
                    .AllowUserToAddRows = False
                    .BackgroundColor = Color.White
                    .ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing
                End With
                EnableDoubleBuffered(DataGridView1)
            Else
                Call RemoveBindingSourceForDGV(DataGridView1)
            End If
    
        End Sub
    Edit: sorry, I forgot to say about the DB; no, the save is going to happen later when the user is finished with all the parameters.
    Last edited by Grand; Aug 17th, 2021 at 07:50 AM.

  6. #6
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,684

    Re: Deleting a row from the data source (datatable) of a datagridview

    Here is a code sample which does a soft delete, minor change to do a hard delete. Code sample written in VS2019, .NET Core 5, will work with older Frameworks.

  7. #7

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2018
    Posts
    514

    Re: Deleting a row from the data source (datatable) of a datagridview

    Quote Originally Posted by kareninstructor View Post
    Here is a code sample which does a soft delete, minor change to do a hard delete. Code sample written in VS2019, .NET Core 5, will work with older Frameworks.
    Thank you.
    Does this remove the row from the datatable?

  8. #8
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,684

    Re: Deleting a row from the data source (datatable) of a datagridview

    Yes the code removed the DataRow from the DataTable

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