Description:
The button is added in the data grid column. We will see how display and save data from a database. It will also show how we can edit, delete the data in database through data grid.
Imports System.Windows.Forms
Imports System.Data
Imports System.Data.SqlClient
Public Class griddb
Dim con As New SqlConnection
As New SqlCommand
Dim da As New SqlDataAdapter
Dim ds As New DataSet
Private Sub griddb_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.Orderdetails2TableAdapter.Fill(Me.ShoppingtrollyDataSet.orderdetails2)
con = New SqlConnection("DataSource=COMPUTER10;Initial Catalog=sample;Integrated Security=True")
con.Open()
cmd = New SqlCommand("select * from books", con)
da = New SqlDataAdapter(cmd)
da.Fill(ds)
DataGridView1.DataSource = ds
'DataGridView1.DataBindings()
con.Close()
End Sub
protected void Item_Click(Object sender, DataGridCommandEventArgs e)
Dim pid,cid As New SqlConnection
TableCell id = e.Item.Cells(1)
pid = id.Text;
TableCell idd = e.Item.Cells(0)
cid = idd.Text;
cmd = New SqlCommand ("deleterecords1", con);
cmddel.CommandType = CommandType.StoredProcedure;
cmddel.Parameters.Add(new SqlParameter("@id", SqlDbType.VarChar, 20))
cmddel.Parameters("@id").Value = pid;
cmddel.Parameters.Add(new SqlParameter("@idd", SqlDbType.VarChar, 20))
cmddel.Parameters("@idd").Value = cid;
con.Open();
cmddel.ExecuteNonQuery();
con.Close();
End Class
|