954,557 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Delete from datagridview

Hi all

I have a situation, i have a form that has a datagridview, on form load, it selects the data from sql db, now what i want is when i right click on one row, i should be able to delete that row and it should also get deleted from the database.

Any help please.

Thanks,
Sacky

sackymatt
Light Poster
29 posts since Dec 2008
Reputation Points: 10
Solved Threads: 0
 

Hi all

I have a situation, i have a form that has a datagridview, on form load, it selects the data from sql db, now what i want is when i right click on one row, i should be able to delete that row and it should also get deleted from the database.

Any help please.

Thanks, Sacky


Iam using vb.net 08

sackymatt
Light Poster
29 posts since Dec 2008
Reputation Points: 10
Solved Threads: 0
 

The the following will explain the Right-Click DataGridView part be sure to read both replies;

http://bytes.com/topic/visual-basic-net/answers/425607-datagridview-force-row-selection-right-click

Then in your Right-Click event you can:

For Each row As DataGridViewRow In DataGridView1.SelectedRows
            DataGridView1.Rows.Remove(row)
     Next


If your DataGridView is bound to a BindingSource which in turn has it's DataSource set to a DataTable of a DataSet filled by a SQLDataAdapter with CommandBuilder, calling the DataAdapter's Update Method will reflect the changes ino your DataGridView back to your SQL DataBase Table.

Phasma
Junior Poster in Training
81 posts since Nov 2008
Reputation Points: 21
Solved Threads: 21
 

Hi Phasma

Thanks for the reply. My datagridview fills with a select statement, there is no datasource abind to it. And i donot just want to right only, i want once i right click, i should be able to delete that row and simutenously delete from the db aswell.

thanks

sackymatt
Light Poster
29 posts since Dec 2008
Reputation Points: 10
Solved Threads: 0
 

For this u need to write procedure to delete row from db or SQlL statement by passing the condition to delete. And load the new set of data and bind back to grid.

Pgmer
Master Poster
714 posts since Apr 2008
Reputation Points: 54
Solved Threads: 121
 
Netcode
Veteran Poster
1,021 posts since Jun 2009
Reputation Points: 43
Solved Threads: 67
 

I thought you wanted to simultaneously delete the row and the record in the database with a minimal of coding. That’s why I suggested the BindingSource, Dataset, DataAdapter scenario. Yes this is OLEDB not SQL but they work pretty much the same. The following is the guts of it I have attached the entire code if you wish to view it. Tested on XP Pro SP3 and VB.net 2008 Framework 3.5

'Global Variables

Dim wrkDir As String = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly.Location())
  Dim da As New OleDbDataAdapter
  Dim ds As New DataSet
  Dim bs As New BindingSource
  Dim edit As Boolean
  'Coupon_Tracker.mdb
  Dim conn As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;" & _
                                  "Data Source=" & wrkDir & "\Coupon_Tracker.mdb")
Private Sub Form1_Load(ByVal sender As Object, _
   ByVal e As System.EventArgs) Handles Me.Load
    'Set the dataGridView's DataSource
    dgv1.DataSource = bs
  End Sub

Private Sub btnLoadAll_Click(ByVal sender As System.Object, _
 ByVal e As System.EventArgs) _
 Handles btnLoadAll.Click
    ds.Tables.Clear()

    Dim sql As String = "SELECT * FROM(Extensions);"

    Dim cmd As New OleDbCommand(sql, conn)

    da.SelectCommand = cmd

   'Have to have this to be able to do Updates
    Dim cmdBuilder As New OleDbCommandBuilder(da)
    da.Fill(ds, "EXTENSIONS")
    bs.DataSource = ds.Tables(0)

  End Sub

Private Sub dgv1_CellMouseClick(ByVal sender As Object, _
                                           ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) _
                                           Handles dgv1.CellMouseClick
    Dim res As DialogResult
    If e.Button = Windows.Forms.MouseButtons.Right AndAlso e.RowIndex >= 0 Then
      dgv1.Rows(e.RowIndex).Selected = True
      res = MessageBox.Show("Are you sure you want to delete Row: " & e.RowIndex & "?", _
                            "REMOVE ROW?", MessageBoxButtons.YesNo)

    End If


    If res = Windows.Forms.DialogResult.Yes Then
      For Each row As DataGridViewRow In dgv1.SelectedRows
        dgv1.Rows.Remove(row)
        edit = True
        Call EditDataBase()
      Next
    End If

  End Sub

  Private Sub EditDataBase()

    If edit Then
      da.Update(ds, "EXTENSIONS")
      edit = False
    End If

  End Sub
Attachments Code.txt (5.47KB)
Phasma
Junior Poster in Training
81 posts since Nov 2008
Reputation Points: 21
Solved Threads: 21
 

Thanks guys for your assistance, it really helped me to arrive to the answer and i got it right.

Thanks

sackymatt
Light Poster
29 posts since Dec 2008
Reputation Points: 10
Solved Threads: 0
 

Cool :) Then plz dont forget to mark the thread as solved.

Pgmer
Master Poster
714 posts since Apr 2008
Reputation Points: 54
Solved Threads: 121
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You