CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Jun 2009
    Posts
    35

    [RESOLVED] Delete Row From DataGridView

    hello frnds, I bound the DataGridView with Database.I have checkboxes in the first column.

    I want that on button click,delete rows whose checboxes are checked,Can somebody tell me???

  2. #2
    Join Date
    Jul 2009
    Location
    Kuwait
    Posts
    170

    Re: Delete Row From DataGridView

    check this code. The click on the first column will make the check box selected or not selected. Then when you press the delete button, the rows where the checkbox status is true will get deleted.

    Code:
        Private Sub DataGridView1_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles empview.CellContentClick
            Try
    
    
                Dim x, y As Short
                Dim bcall As Boolean
    
                x = DataGridView .CurrentCellAddress.X
                y = DataGridView .CurrentCellAddress.Y
    
                If x = 0 Then
                    bcall = DataGridView .Item(x, y).Value
    
                    If bcall = True Then
                        DataGridView .Item(x, y).Value = False
                    Else
                        DataGridView .Item(x, y).Value = True
                    End If
    
                End If
    
            Catch ex As Exception
    
                MsgBox(ex.Message)
    
    Private Sub btnDlt_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDlt.Click
       For j = 0 To DataGridView .RowCount - 1
    
                    bcall = DataGridView .Item(0, j).Value
                    If (bcall = True) Then
                        empid = DataGridView .Item(2, j).Value
     cmd = New OleDbCommand(" delete from employee  where empid = " & empid & " ", cn)
                            cmd.ExecuteNonQuery()
    
            End Try
        End Sub

  3. #3
    Join Date
    Jun 2009
    Posts
    35

    Re: Delete Row From DataGridView

    Thx makdu,very Much. Hi makdu, I want to update data in Gridview,Suppose we double click on cell & edit text,I want the data to be updated in Database also,In which event,I write the code for updation.Can You tell me?
    Last edited by sonia.sardana; October 27th, 2009 at 07:25 AM.

  4. #4
    Join Date
    Jul 2009
    Location
    Kuwait
    Posts
    170

    Re: [RESOLVED] Delete Row From DataGridView

    first you have to write the needed steps to edit the text on double clik
    Code:
      Private Sub empview_CellContentDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles empview.CellContentDoubleClick
     Dim x, y As Short
             
    
                x = DataGridView .CurrentCellAddress.X
                y = DataGridView .CurrentCellAddress.Y
    txtbox1.text= DataGridView .Item(x, y).Value
    
        End Sub
    now have a submit button which update the value to the database with the value in txtbox .

  5. #5
    Join Date
    Apr 2010
    Posts
    41

    Re: [RESOLVED] Delete Row From DataGridView

    Hi,
    I am also trying to delete a row by a button click. I added a new column with a checkbox which I am going to use like this:
    Code:
    For Each row As DataGridViewRow In Database_installDataGridView.Rows
                If Not row.IsNewRow Then
                    If row.Cells(Checkbox.Index).Value = True Then
                        MsgBox("checked")
                       Database_installDataGridView.Rows.Remove(row)
                     
                    Else
                        MsgBox("0")
                    End If
                End If
            Next
    But it just doesnt delete the row and sometimes comes with an error:
    system.indexoutofrangeexeption

  6. #6
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: [RESOLVED] Delete Row From DataGridView

    Start a new thread. This one was RESOLVED.
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

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

Featured