Here i have created sample in this DataGridView Populates the Employee Details, also we can update details of Employee through the update button inside the DataGridView.
I hope this will help you out.
Namespaces
using System.Data.SqlClient;
using System.Configuration;
C#
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
BindEmployees();
}
private void BindEmployees()
{
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("SELECT TOP 5 Id,Name,Address FROM Employees", con))
{
using (SqlDataAdapter da = new SqlDataAdapter(cmd))
{
gvEmployees.AllowUserToAddRows = false;
DataTable dt = new System.Data.DataTable();
da.Fill(dt);
gvEmployees.Columns.Clear();
this.gvEmployees.DataSource = dt;
DataGridViewButtonColumn editButton;
editButton = new DataGridViewButtonColumn();
editButton.HeaderText = "Update";
editButton.Text = "Update";
editButton.UseColumnTextForButtonValue = true;
editButton.Width = 80;
gvEmployees.Columns.Add(editButton);
for (int i = 0; i < gvEmployees.Rows.Count; i++)
{
gvEmployees.Rows[i].Cells[0].ReadOnly = true;
}
}
}
}
}
private void gvEmployees_CellClick(object sender, DataGridViewCellEventArgs e)
{
int currentRow = int.Parse(e.RowIndex.ToString());
int currentColumnIndex = int.Parse(e.ColumnIndex.ToString());
if (currentColumnIndex == 3)
{
string id = gvEmployees.Rows[currentRow].Cells[0].Value.ToString();
string name = gvEmployees.Rows[currentRow].Cells[1].Value.ToString();
string address = gvEmployees.Rows[currentRow].Cells[2].Value.ToString();
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("Update Employees set Name=@Name,Address=@Address Where Id=@Id", con))
{
cmd.Parameters.AddWithValue("@Id", id);
cmd.Parameters.AddWithValue("@Name", name);
cmd.Parameters.AddWithValue("@Address", address);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
MessageBox.Show("Emplyoee Details Successfully Saved ");
BindEmployees();
}
}
}
}
}
VB.Net
Namespaces
Imports System.Configuration
Imports System.Data.SqlClient
VB
Private constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Public Sub New()
InitializeComponent()
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
BindEmployees()
End Sub
Private Sub BindEmployees()
Using con As New SqlConnection(constr)
con.Open()
Using cmd As New SqlCommand("SELECT TOP 5 Id,Name,Address FROM Employees", con)
Using da As New SqlDataAdapter(cmd)
gvEmployees.AllowUserToAddRows = False
Dim dt As DataTable = New System.Data.DataTable()
da.Fill(dt)
con.Close()
gvEmployees.Columns.Clear()
Me.gvEmployees.DataSource = dt
Dim editButton As DataGridViewButtonColumn
editButton = New DataGridViewButtonColumn()
editButton.HeaderText = "Update"
editButton.Text = "Update"
editButton.UseColumnTextForButtonValue = True
editButton.Width = 80
gvEmployees.Columns.Add(editButton)
For i As Integer = 0 To gvEmployees.Rows.Count - 1
gvEmployees.Rows(i).Cells(0).[ReadOnly] = True
Next
End Using
End Using
End Using
End Sub
Private Sub gvEmployees_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles gvEmployees.CellClick
Dim currentRow As Integer = Integer.Parse(e.RowIndex.ToString())
Dim currentColumnIndex As Integer = Integer.Parse(e.ColumnIndex.ToString())
If currentColumnIndex = 3 Then
Dim id As String = gvEmployees.Rows(currentRow).Cells(0).Value.ToString()
Dim name As String = gvEmployees.Rows(currentRow).Cells(1).Value.ToString()
Dim address As String = gvEmployees.Rows(currentRow).Cells(2).Value.ToString()
Using con As New SqlConnection(constr)
Using cmd As New SqlCommand("Update Employees set Name=@Name,Address=@Address Where Id=@Id", con)
cmd.Parameters.AddWithValue("@Id", id)
cmd.Parameters.AddWithValue("@Name", name)
cmd.Parameters.AddWithValue("@Address", address)
con.Open()
cmd.ExecuteNonQuery()
con.Close()
MessageBox.Show("Emplyoee Details Successfully Saved ")
BindEmployees()
End Using
End Using
End If
End Sub
Screenshot
