Click here to Skip to main content
16,016,527 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hai guys, I created a form to insert, update, delete.
And i am using datagridview control to view the database updation.
Here what i actually need is,
If i click a row from datagridview and click delete it should delete.
i need code for this
Help me,

Regards Sissy Ram
Posted

VB
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If Me.DataGridView1.SelectedRows.Count > 0 Then
            GetValueByQuery("delete * from table1 where id=" & Me.DataGridView1.SelectedRows(0).Cells(0).Value)
        End If
    End Sub
 
Share this answer
 
Comments
Sissy Ram 24-Feb-14 11:39am    
that is ok cool sir i have another problem in my form.
if i make upadation in runtime it works.
but after restarting the form the updation are not diplaying.
and here is my code
Imports System.Data
Imports System.Data.OleDb
Public Class _01_TAILOR_SETUP
Dim con As OleDbConnection
Dim cmd As OleDbCommand
Dim da As OleDbDataAdapter
Dim ds As DataSet
Private Sub _01_TAILOR_SETUP_FormClosed(sender As Object, e As FormClosedEventArgs) Handles Me.FormClosed

End Sub

Private Sub _01_TAILOR_SETUP_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
Dim result As DialogResult = MsgBox("do you want to close?", MsgBoxStyle.YesNo)
If result = Windows.Forms.DialogResult.Yes Then
e.Cancel = True
_00_MAIN.Show()
Me.Hide()
ElseIf result = Windows.Forms.DialogResult.No Then
e.Cancel = True
End If
End Sub

Private Sub _01_TAILOR_SETUP_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'TODO: This line of code loads data into the 'DBDataSet.TAILOR' table. You can move, or remove it, as needed.
' Me.TAILORTableAdapter.Fill(Me.DBDataSet.TAILOR)
ComboBox1.Items.Add("Available")
ComboBox1.Items.Add("Terminated")
ComboBox1.Items.Add("Both")
con = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=DB.accdb")
con.Open()
da = New OleDbDataAdapter("Select * from tailor", con)
ds = New DataSet()
da.Fill(ds, "tailor")
DataGridView1.DataSource = ds.Tables(0)
con.Close()
End Sub


Private Sub ComboBox1_KeyDown(sender As Object, e As KeyEventArgs) Handles ComboBox1.KeyDown

If e.KeyCode = Keys.Escape Then
If TextBox1.Text = "" And TextBox2.Text = "" And TextBox3.Text = "" And ComboBox1.Text = "Select value" Then
MsgBox("Fill appropriate values")
TextBox3.Focus()
Else
Dim result As DialogResult = MsgBox("Do you want to save", MsgBoxStyle.YesNo)
If result = Windows.Forms.DialogResult.Yes Then
con.Open()
cmd = New OleDbCommand("INSERT INTO Tailor (TCODE, TNAME, TADDRESS,STATUS) VALUES ('" & TextBox3.Text & "', '" & TextBox2.Text & "', '" & TextBox1.Text & "', '" & ComboBox1.Text & "');", con)
cmd.ExecuteNonQuery()
MsgBox("INSERTED")
da = New OleDbDataAdapter("Select * from tailor", con)
ds = New DataSet()
da.Fill(ds, "tailor")
DataGridView1.DataSource = ds.Tables(0)
con.Close()
ElseIf result = Windows.Forms.DialogResult.No Then
End If
End If
End If
End Sub


End Class
i need to resolve this please help me
sohail awr 25-Feb-14 9:23am    
Imports System.Data
Imports System.Data.OleDb
Public Class _01_TAILOR_SETUP
Dim con As OleDbConnection = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=DB.accdb")
Dim cmd As OleDbCommand
Dim da As OleDbDataAdapter
Dim ds As DataSet

Private Sub _01_TAILOR_SETUP_FormClosed(ByVal sender As Object, ByVal e As FormClosedEventArgs) Handles Me.FormClosed

End Sub

Private Sub _01_TAILOR_SETUP_FormClosing(ByVal sender As Object, ByVal e As FormClosingEventArgs) Handles Me.FormClosing
Dim result As DialogResult = MsgBox("do you want to close?", MsgBoxStyle.YesNo)
If result = Windows.Forms.DialogResult.Yes Then
e.Cancel = True
_00_MAIN.Show()
Me.Hide()
ElseIf result = Windows.Forms.DialogResult.No Then
e.Cancel = True
End If
End Sub

Private Sub _01_TAILOR_SETUP_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load

ComboBox1.Items.Add("Available")
ComboBox1.Items.Add("Terminated")
ComboBox1.Items.Add("Both")

fill_grid() 'Use this type of trick so it will reduce your lines of code.
End Sub


Private Sub ComboBox1_KeyDown(ByVal sender As Object, ByVal e As KeyEventArgs) Handles ComboBox1.KeyDown

If e.KeyCode = Keys.Escape Then
If TextBox1.Text = "" And TextBox2.Text = "" And TextBox3.Text = "" And ComboBox1.Text = "Select value" Then
MsgBox("Fill appropriate values")
TextBox3.Focus()
Else
Dim result As DialogResult = MsgBox("Do you want to save", MsgBoxStyle.YesNo)
If result = Windows.Forms.DialogResult.Yes Then
con.Open()
cmd = New OleDbCommand("INSERT INTO Tailor (TCODE, TNAME, TADDRESS,STATUS) VALUES ('" & TextBox3.Text & "', '" & TextBox2.Text & "', '" & TextBox1.Text & "', '" & ComboBox1.Text & "');", con)
cmd.ExecuteNonQuery()
MsgBox("INSERTED")
fill_grid() 'Use this type of trick so it will reduce your lines of code.
ElseIf result = Windows.Forms.DialogResult.No Then
End If
End If
End If
End Sub

Sub fill_grid()
If con.State = ConnectionState.Closed Then
con.Open()
End If
da = New OleDbDataAdapter("Select * from Tailor", con)
ds = New DataSet()
da.Fill(ds, "tailor")
DataGridView1.DataSource = ds.Tables(0)
con.Close()
End Sub

Private Sub btn_delete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_delete.Click

If Me.DataGridView1.SelectedRows.Count > 0 Then
If con.State = ConnectionState.Closed Then
con.Open()
End If
cmd = New OleDbCommand("Delete * from Tailor where TCODE='" & Me.DataGridView1.SelectedRows(0).Cells(0).Value & "'", con)
cmd.ExecuteNonQuery()
MsgBox("Recodrd Deleted!")
fill_grid()
con.Close()
End If

End Sub
End Class
Sissy Ram 24-Feb-14 11:45am    
Should i wanna to add any files to support GetByValue query?
Because it shows as an error GetByValueQuery is not declared
Ohh actually getValuebyQuery is my own function I am enclosing here with so that you can use it. In this function you can use select/insert/update/delete query as you required..

VB
<pre lang="vb">   Public oCon As New System.Data.OleDb.OleDbConnection("provider=microsoft.jet.OLEDB.4.0;data source=" & Application.StartupPath & "\Data\mydata.mdb")

Function GetValueByQuery(ByVal Query As String) As String
        Dim temp As String
        Dim ocom As New OleDbCommand
        Dim oRead As OleDbDataReader
        If oCon.State = ConnectionState.Closed Then
            oCon.Open()
        End If

        ocom.Connection = oCon
        ocom.CommandText = Query

        oRead = ocom.ExecuteReader
        If oRead.HasRows = True Then
            oRead.Read()
            If IsDBNull(oRead(0)) = True Then
                temp = &quot;0&quot;
            Else
                temp = oRead(0)
            End If
            oRead.Close()

        Else
            temp = &quot;0&quot;
            oRead.Close()
        End If
        Return temp
    End Function</pre>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900