Hi Waghmare,
Please refer below sample.
Database
I have made use of the following table Customers with the schema as follows.
I have already inserted few records in the table.
You can download the database table SQL by clicking the download link below.
Download SQL file
Namespaces
C#
using System.Data;
using System.Data.SqlClient;
VB.Net
Imports System.Data
Imports System.Data.SqlClient
Code
C#
public Form1()
{
InitializeComponent();
string ConnectionString = @"Data Source=.;Initial Catalog=Test;User id = sa;password=pass@123";
using (SqlConnection con = new SqlConnection(ConnectionString))
{
using (SqlCommand cmd = new SqlCommand("SELECT CustomerId, Name, Country FROM Customers", con))
{
cmd.CommandType = CommandType.Text;
using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
{
using (DataTable dt = new DataTable())
{
sda.Fill(dt);
BindGrid(dt);
}
}
}
}
}
private void BindGrid(DataTable dt)
{
//Hide the last blank line.
gvCustomers.AllowUserToAddRows = false;
//Clear Columns.
gvCustomers.Columns.Clear();
//Add Columns.
DataGridViewColumn customerId = new DataGridViewTextBoxColumn();
customerId.Name = "CustomerId";
customerId.HeaderText = "CustomerId";
customerId.DataPropertyName = "CustomerId";
customerId.Width = 100;
gvCustomers.Columns.Insert(0, customerId);
DataGridViewColumn name = new DataGridViewTextBoxColumn();
name.HeaderText = "Name";
name.Name = "Name";
name.DataPropertyName = "Name";
name.Width = 100;
gvCustomers.Columns.Insert(1, name);
DataGridViewColumn country = new DataGridViewTextBoxColumn();
country.Name = "Country";
country.HeaderText = "Country";
country.DataPropertyName = "Country";
country.Width = 100;
gvCustomers.Columns.Insert(2, country);
//Bind the DataGridView.
gvCustomers.DataSource = null;
gvCustomers.DataSource = dt;
//Add the Button Column.
DataGridViewButtonColumn buttonColumn = new DataGridViewButtonColumn();
buttonColumn.HeaderText = "Modify";
buttonColumn.Width = 60;
buttonColumn.Name = "buttonColumn";
buttonColumn.Text = "Edit";
buttonColumn.UseColumnTextForButtonValue = true;
gvCustomers.Columns.Insert(0, buttonColumn);
}
protected void OnSave(object sender, EventArgs e)
{
DataTable dt = gvCustomers.DataSource as DataTable;
BindGrid(dt);
}
VB.Net
Public Sub New()
InitializeComponent()
Dim ConnectionString As String = "Data Source=.;Initial Catalog=Test;User id = sa;password=pass@123"
Using con As SqlConnection = New SqlConnection(ConnectionString)
Using cmd As SqlCommand = New SqlCommand("SELECT CustomerId, Name, Country FROM Customers", con)
cmd.CommandType = CommandType.Text
Using sda As SqlDataAdapter = New SqlDataAdapter(cmd)
Using dt As DataTable = New DataTable()
sda.Fill(dt)
BindGrid(dt)
End Using
End Using
End Using
End Using
End Sub
Private Sub BindGrid(ByVal dt As DataTable)
gvCustomers.AllowUserToAddRows = False
gvCustomers.Columns.Clear()
Dim customerId As DataGridViewColumn = New DataGridViewTextBoxColumn()
customerId.Name = "CustomerId"
customerId.HeaderText = "CustomerId"
customerId.DataPropertyName = "CustomerId"
customerId.Width = 100
gvCustomers.Columns.Insert(0, customerId)
Dim name As DataGridViewColumn = New DataGridViewTextBoxColumn()
name.HeaderText = "Name"
name.Name = "Name"
name.DataPropertyName = "Name"
name.Width = 100
gvCustomers.Columns.Insert(1, name)
Dim country As DataGridViewColumn = New DataGridViewTextBoxColumn()
country.Name = "Country"
country.HeaderText = "Country"
country.DataPropertyName = "Country"
country.Width = 100
gvCustomers.Columns.Insert(2, country)
gvCustomers.DataSource = Nothing
gvCustomers.DataSource = dt
Dim buttonColumn As DataGridViewButtonColumn = New DataGridViewButtonColumn()
buttonColumn.HeaderText = "Modify"
buttonColumn.Width = 60
buttonColumn.Name = "buttonColumn"
buttonColumn.Text = "Edit"
buttonColumn.UseColumnTextForButtonValue = True
gvCustomers.Columns.Insert(0, buttonColumn)
End Sub
Protected Sub OnSave(ByVal sender As Object, ByVal e As EventArgs)
Dim dt As DataTable = TryCast(gvCustomers.DataSource, DataTable)
BindGrid(dt)
End Sub
Screenshot