Hi Rezu2215,
Refer below sample.
Namespaces
C#
using System.Data.SqlClient;
VB.Net
Imports System.Data.SqlClient
Code
C#
private void Form1_Load(object sender, EventArgs e)
{
txtCustomerId.Enabled = false;
BindGrid();
DataGridViewLinkColumn btnUpdate = new DataGridViewLinkColumn();
btnUpdate.UseColumnTextForLinkValue = true;
btnUpdate.LinkBehavior = LinkBehavior.SystemDefault;
btnUpdate.HeaderText = "Button";
btnUpdate.Name = "txtName";
btnUpdate.Text = "Edit";
btnUpdate.Width = 50;
dataGridView1.Columns.Insert(0, btnUpdate);
dataGridView1.CellContentClick += new DataGridViewCellEventHandler(DataGridView_CellClick);
}
private void BindGrid()
{
SqlConnection con = new SqlConnection(constr);
SqlCommand cmd = new SqlCommand("SELECT CustomerId, Name, Country FROM Customers", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
this.dataGridView1.DataSource = dt;
}
private void DataGridView_CellClick(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == dataGridView1.Columns["txtName"].Index)
{
if (e.RowIndex >= 0)
{
DataGridViewRow row = dataGridView1.Rows[e.RowIndex];
txtCustomerId.Text = row.Cells[1].Value.ToString();
txtName.Text = row.Cells[2].Value.ToString();
txtCountry.Text = row.Cells[3].Value.ToString();
}
}
}
private void Update(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(constr);
SqlCommand cmd = new SqlCommand("UPDATE Customers SET Name = @Name WHERE CustomerId = @CustomerId", con);
cmd.Parameters.AddWithValue("@CustomerId", txtCustomerId.Text);
cmd.Parameters.AddWithValue("@Name", txtName.Text);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
BindGrid();
}
VB.Net
Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs)
txtCustomerId.Enabled = False
BindGrid()
Dim btnUpdate As DataGridViewLinkColumn = New DataGridViewLinkColumn()
btnUpdate.UseColumnTextForLinkValue = True
btnUpdate.LinkBehavior = LinkBehavior.SystemDefault
btnUpdate.HeaderText = "Button"
btnUpdate.Name = "txtName"
btnUpdate.Text = "Edit"
btnUpdate.Width = 50
dataGridView1.Columns.Insert(0, btnUpdate)
AddHandler dataGridView1.CellContentClick, AddressOf DataGridView_CellClick
End Sub
Private Sub BindGrid()
Dim con As SqlConnection = New SqlConnection(constr)
Dim cmd As SqlCommand = New SqlCommand("SELECT CustomerId, Name, Country FROM Customers", con)
Dim da As SqlDataAdapter = New SqlDataAdapter(cmd)
Dim dt As DataTable = New DataTable()
da.Fill(dt)
Me.dataGridView1.DataSource = dt
End Sub
Private Sub DataGridView_CellClick(ByVal sender As Object, ByVal e As DataGridViewCellEventArgs)
If e.ColumnIndex = dataGridView1.Columns("txtName").Index Then
If e.RowIndex >= 0 Then
Dim row As DataGridViewRow = dataGridView1.Rows(e.RowIndex)
txtCustomerId.Text = row.Cells(1).Value.ToString()
txtName.Text = row.Cells(2).Value.ToString()
txtCountry.Text = row.Cells(3).Value.ToString()
End If
End If
End Sub
Private Sub Update(ByVal sender As Object, ByVal e As EventArgs) Handles button1.Click
Dim con As SqlConnection = New SqlConnection(constr)
Dim cmd As SqlCommand = New SqlCommand("UPDATE Customers SET Name = @Name WHERE CustomerId = @CustomerId", con)
cmd.Parameters.AddWithValue("@CustomerId", txtCustomerId.Text)
cmd.Parameters.AddWithValue("@Name", txtName.Text)
con.Open()
cmd.ExecuteNonQuery()
con.Close()
BindGrid()
End Sub
Screenshot
