Hi simflex,
Check this sample.
HTML
<div>
<b>GridView 1</b>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" ShowFooter="true"
OnRowEditing="OnRowEditing" OnRowCancelingEdit="OnRowCancelingEdit" OnRowUpdating="OnRowUpdating"
OnRowDeleting="OnRowdeleting">
<Columns>
<asp:TemplateField HeaderText="ID">
<ItemTemplate>
<asp:Label ID="lblID" Text='<%# Eval("CustomerID") %>' runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<%# Eval("Name") %>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtItemName" Text=' <%# Eval("Name") %>' runat="server" />
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtFooterName" Text=' <%# Eval("Name") %>' runat="server" />
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Country">
<ItemTemplate>
<%# Eval("Country") %>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtItemCountry" Text=' <%# Eval("Country") %>' runat="server" />
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtFooterCountry" Text='<%# Eval("Country") %>' runat="server" />
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
</ItemTemplate>
<FooterTemplate>
<asp:Button ID="btnAdd" Text="Add" runat="server" OnClick="Add" CommandName="Footer" />
</FooterTemplate>
</asp:TemplateField>
<asp:CommandField ShowEditButton="true" ShowDeleteButton="true" />
</Columns>
<AlternatingRowStyle />
<EmptyDataTemplate>
<tr>
<th>ID</th>
<th>Name</th>
<th>Country</th>
</tr>
<tr>
<td></td>
<td><asp:TextBox ID="txtName" runat="server" /></td>
<td><asp:TextBox ID="txtCountry" runat="server" /></td>
<td><asp:Button ID="btnAdd" runat="server" Text="Add" OnClick="Add" CommandName="EmptyDataTemplate" /></td>
</tr>
</EmptyDataTemplate>
</asp:GridView><br />
<b>GridView 2</b>
<asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="false" ShowFooter="true"
OnRowEditing="GridView2_OnRowEditing" OnRowCancelingEdit="GridView2_OnRowCancelingEdit"
OnRowUpdating="GridView2_OnRowUpdating" OnRowDeleting="GridView2_OnRowdeleting">
<Columns>
<asp:TemplateField HeaderText="ID">
<ItemTemplate>
<asp:Label ID="lblEmpID" Text='<%# Eval("EmpID") %>' runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<%# Eval("EmpName")%>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtItemName" Text=' <%# Eval("EmpName") %>' runat="server" />
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtFooterName" Text=' <%# Eval("EmpName") %>' runat="server" />
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Designation">
<ItemTemplate>
<%# Eval("Designation")%>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtItemDesignation" Text=' <%# Eval("Designation") %>' runat="server" />
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtFooterDesignation" Text='<%# Eval("Designation") %>' runat="server" />
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Group Name" Visible="false">
<ItemTemplate>
<%# Eval("GroupName")%>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtItemGroupName" Text=' <%# Eval("GroupName") %>' runat="server" />
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtFooterGroupName" Text='<%# Eval("GroupName") %>' runat="server" />
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField>
<FooterTemplate>
<asp:Button ID="btnAdd" Text="Add" runat="server" OnClick="OnAdd" CommandName="Footer" />
</FooterTemplate>
</asp:TemplateField>
<asp:CommandField ShowEditButton="true" ShowDeleteButton="true" />
</Columns>
<AlternatingRowStyle />
<EmptyDataTemplate>
<tr>
<th>ID</th>
<th>Employee Name</th>
<th>Designation</th>
<th>Group Name</th>
</tr>
<tr>
<td></td>
<td><asp:TextBox ID="txtName" runat="server" /></td>
<td><asp:TextBox ID="txtDesignation" runat="server" /></td>
<td><asp:TextBox ID="txtGroupName" runat="server" /></td>
<td><asp:Button ID="btnAdd" runat="server" Text="Add" OnClick="Add" CommandName="EmptyDataTemplate" /></td>
</tr>
</EmptyDataTemplate>
</asp:GridView>
</div>
Namespaces
C#
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
VB.Net
Imports System.Configuration
Imports System.Data
Imports System.Data.SqlClient
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
this.BindCustomers();
this.BindEmployees();
}
}
//-------------- GridView1 --------------------
protected void Add(object sender, EventArgs e)
{
Control control = null;
if (GridView1.FooterRow != null)
{
control = GridView1.FooterRow;
}
else
{
control = GridView1.Controls[0].Controls[0];
}
string name = (control.FindControl("txtFooterName") as TextBox).Text;
string country = (control.FindControl("txtFooterCountry") as TextBox).Text;
using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["constr"].ConnectionString))
{
using (SqlCommand cmd = new SqlCommand("INSERT INTO Customers VALUES(@Name,@Country)", con))
{
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("@Name", name);
cmd.Parameters.AddWithValue("@Country", country);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
Response.Redirect(Request.Url.AbsoluteUri);
}
protected void OnRowEditing(object sender, GridViewEditEventArgs e)
{
this.GridView1.EditIndex = e.NewEditIndex;
this.BindCustomers();
}
protected void OnRowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
this.GridView1.EditIndex = -1;
this.BindCustomers();
}
protected void OnRowUpdating(object sender, GridViewUpdateEventArgs e)
{
GridViewRow row = GridView1.Rows[e.RowIndex];
string id = (row.FindControl("lblID") as Label).Text;
string name = (row.FindControl("txtItemName") as TextBox).Text;
string country = (row.FindControl("txtItemCountry") as TextBox).Text;
using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["constr"].ConnectionString))
{
using (SqlCommand cmd = new SqlCommand("UPDATE Customers SET Name=@Name, Country=@Country WHERE CustomerID=@CustomerID", con))
{
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("@Name", name);
cmd.Parameters.AddWithValue("@Country", country);
cmd.Parameters.AddWithValue("@CustomerID", id);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
this.GridView1.EditIndex = -1;
this.BindCustomers();
}
protected void OnRowdeleting(object sender, GridViewDeleteEventArgs e)
{
GridViewRow row = GridView1.Rows[e.RowIndex];
string id = (row.FindControl("lblID") as Label).Text;
using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["constr"].ConnectionString))
{
using (SqlCommand cmd = new SqlCommand("DELETE FROM Customers WHERE CustomerID=@CustomerID", con))
{
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("@CustomerID", id);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
this.BindCustomers();
}
//----------------- GridView1 End ------------
//------------- GridView2 ----------------
protected void OnAdd(object sender, EventArgs e)
{
Control control = null;
if (GridView2.FooterRow != null)
{
control = GridView2.FooterRow;
}
else
{
control = GridView2.Controls[0].Controls[0];
}
string name = (control.FindControl("txtFooterName") as TextBox).Text;
string designation = (control.FindControl("txtFooterDesignation") as TextBox).Text;
string groupName = (control.FindControl("txtFooterGroupName") as TextBox).Text;
using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["constr"].ConnectionString))
{
using (SqlCommand cmd = new SqlCommand("INSERT INTO Employee VALUES(@EmpName,@Designation,@GroupName)", con))
{
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("@EmpName", name);
cmd.Parameters.AddWithValue("@Designation", designation);
cmd.Parameters.AddWithValue("@GroupName", groupName);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
Response.Redirect(Request.Url.AbsoluteUri);
}
protected void GridView2_OnRowEditing(object sender, GridViewEditEventArgs e)
{
this.GridView2.EditIndex = e.NewEditIndex;
this.BindEmployees();
}
protected void GridView2_OnRowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
this.GridView2.EditIndex = -1;
this.BindEmployees();
}
protected void GridView2_OnRowUpdating(object sender, GridViewUpdateEventArgs e)
{
GridViewRow row = GridView2.Rows[e.RowIndex];
string id = (row.FindControl("lblEmpID") as Label).Text;
string name = (row.FindControl("txtItemName") as TextBox).Text;
string designation = (row.FindControl("txtItemDesignation") as TextBox).Text;
string groupName = (row.FindControl("txtItemGroupName") as TextBox).Text;
using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["constr"].ConnectionString))
{
using (SqlCommand cmd = new SqlCommand("UPDATE Employee SET EmpName=@EmpName, Designation=@Designation , GroupName=@GroupName WHERE EmpId=@EmpId", con))
{
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("@EmpName", name);
cmd.Parameters.AddWithValue("@Designation", designation);
cmd.Parameters.AddWithValue("@GroupName", groupName);
cmd.Parameters.AddWithValue("@EmpId", id);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
this.GridView2.EditIndex = -1;
this.BindEmployees();
}
protected void GridView2_OnRowdeleting(object sender, GridViewDeleteEventArgs e)
{
GridViewRow row = GridView2.Rows[e.RowIndex];
string id = (row.FindControl("lblEmpID") as Label).Text;
using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["constr"].ConnectionString))
{
using (SqlCommand cmd = new SqlCommand("DELETE FROM Employee WHERE EmpId=@EmpId", con))
{
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("@EmpId", id);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
this.BindEmployees();
}
//------------- GridView2 End ------------
private void BindCustomers()
{
using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["constr"].ConnectionString))
{
using (SqlCommand cmd = new SqlCommand("SELECT CustomerID,Name,Country FROM Customers", con))
{
cmd.CommandType = CommandType.Text;
using (SqlDataAdapter sda = new SqlDataAdapter())
{
sda.SelectCommand = cmd;
DataTable dt = new DataTable();
sda.Fill(dt);
this.GridView1.DataSource = dt;
this.GridView1.DataBind();
}
}
}
}
private void BindEmployees()
{
using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["constr"].ConnectionString))
{
using (SqlCommand cmd = new SqlCommand("SELECT EmpId,EmpName,Designation,GroupName FROM Employee", con))
{
cmd.CommandType = CommandType.Text;
using (SqlDataAdapter sda = new SqlDataAdapter())
{
sda.SelectCommand = cmd;
DataTable dt = new DataTable();
sda.Fill(dt);
this.GridView2.DataSource = dt;
this.GridView2.DataBind();
}
}
}
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Me.BindCustomers()
Me.BindEmployees()
End If
End Sub
'------------- GridView1 ------------
Protected Sub Add(ByVal sender As Object, ByVal e As EventArgs)
Dim control As Control = Nothing
If GridView1.FooterRow IsNot Nothing Then
control = GridView1.FooterRow
Else
control = GridView1.Controls(0).Controls(0)
End If
Dim name As String = (TryCast(control.FindControl("txtFooterName"), TextBox)).Text
Dim country As String = (TryCast(control.FindControl("txtFooterCountry"), TextBox)).Text
Using con As SqlConnection = New SqlConnection(ConfigurationManager.ConnectionStrings("constr").ConnectionString)
Using cmd As SqlCommand = New SqlCommand("INSERT INTO Customers VALUES(@Name,@Country)", con)
cmd.CommandType = CommandType.Text
cmd.Parameters.AddWithValue("@Name", name)
cmd.Parameters.AddWithValue("@Country", country)
con.Open()
cmd.ExecuteNonQuery()
con.Close()
End Using
End Using
Response.Redirect(Request.Url.AbsoluteUri)
End Sub
Protected Sub OnRowEditing(ByVal sender As Object, ByVal e As GridViewEditEventArgs)
Me.GridView1.EditIndex = e.NewEditIndex
Me.BindCustomers()
End Sub
Protected Sub OnRowCancelingEdit(ByVal sender As Object, ByVal e As GridViewCancelEditEventArgs)
Me.GridView1.EditIndex = -1
Me.BindCustomers()
End Sub
Protected Sub OnRowUpdating(ByVal sender As Object, ByVal e As GridViewUpdateEventArgs)
Dim row As GridViewRow = GridView1.Rows(e.RowIndex)
Dim id As String = (TryCast(row.FindControl("lblID"), Label)).Text
Dim name As String = (TryCast(row.FindControl("txtItemName"), TextBox)).Text
Dim country As String = (TryCast(row.FindControl("txtItemCountry"), TextBox)).Text
Using con As SqlConnection = New SqlConnection(ConfigurationManager.ConnectionStrings("constr").ConnectionString)
Using cmd As SqlCommand = New SqlCommand("UPDATE Customers SET Name=@Name, Country=@Country WHERE CustomerID=@CustomerID", con)
cmd.CommandType = CommandType.Text
cmd.Parameters.AddWithValue("@Name", name)
cmd.Parameters.AddWithValue("@Country", country)
cmd.Parameters.AddWithValue("@CustomerID", id)
con.Open()
cmd.ExecuteNonQuery()
con.Close()
End Using
End Using
Me.GridView1.EditIndex = -1
Me.BindCustomers()
End Sub
Protected Sub OnRowdeleting(ByVal sender As Object, ByVal e As GridViewDeleteEventArgs)
Dim row As GridViewRow = GridView1.Rows(e.RowIndex)
Dim id As String = (TryCast(row.FindControl("lblID"), Label)).Text
Using con As SqlConnection = New SqlConnection(ConfigurationManager.ConnectionStrings("constr").ConnectionString)
Using cmd As SqlCommand = New SqlCommand("DELETE FROM Customers WHERE CustomerID=@CustomerID", con)
cmd.CommandType = CommandType.Text
cmd.Parameters.AddWithValue("@CustomerID", id)
con.Open()
cmd.ExecuteNonQuery()
con.Close()
End Using
End Using
Me.BindCustomers()
End Sub
'------------- GridView1 End------------
'------------- GridView2 ------------
Protected Sub OnAdd(ByVal sender As Object, ByVal e As EventArgs)
Dim control As Control = Nothing
If GridView2.FooterRow IsNot Nothing Then
control = GridView2.FooterRow
Else
control = GridView2.Controls(0).Controls(0)
End If
Dim name As String = (TryCast(control.FindControl("txtFooterName"), TextBox)).Text
Dim designation As String = (TryCast(control.FindControl("txtFooterDesignation"), TextBox)).Text
Dim groupName As String = (TryCast(control.FindControl("txtFooterGroupName"), TextBox)).Text
Using con As SqlConnection = New SqlConnection(ConfigurationManager.ConnectionStrings("constr").ConnectionString)
Using cmd As SqlCommand = New SqlCommand("INSERT INTO Employee VALUES(@EmpName,@Designation,@GroupName)", con)
cmd.CommandType = CommandType.Text
cmd.Parameters.AddWithValue("@EmpName", name)
cmd.Parameters.AddWithValue("@Designation", designation)
cmd.Parameters.AddWithValue("@GroupName", groupName)
con.Open()
cmd.ExecuteNonQuery()
con.Close()
End Using
End Using
Response.Redirect(Request.Url.AbsoluteUri)
End Sub
Protected Sub GridView2_OnRowEditing(ByVal sender As Object, ByVal e As GridViewEditEventArgs)
Me.GridView2.EditIndex = e.NewEditIndex
Me.BindEmployees()
End Sub
Protected Sub GridView2_OnRowCancelingEdit(ByVal sender As Object, ByVal e As GridViewCancelEditEventArgs)
Me.GridView2.EditIndex = -1
Me.BindEmployees()
End Sub
Protected Sub GridView2_OnRowUpdating(ByVal sender As Object, ByVal e As GridViewUpdateEventArgs)
Dim row As GridViewRow = GridView2.Rows(e.RowIndex)
Dim id As String = (TryCast(row.FindControl("lblEmpID"), Label)).Text
Dim name As String = (TryCast(row.FindControl("txtItemName"), TextBox)).Text
Dim designation As String = (TryCast(row.FindControl("txtItemDesignation"), TextBox)).Text
Dim groupName As String = (TryCast(row.FindControl("txtItemGroupName"), TextBox)).Text
Using con As SqlConnection = New SqlConnection(ConfigurationManager.ConnectionStrings("constr").ConnectionString)
Using cmd As SqlCommand = New SqlCommand("UPDATE Employee SET EmpName=@EmpName, Designation=@Designation , GroupName=@GroupName WHERE EmpId=@EmpId", con)
cmd.CommandType = CommandType.Text
cmd.Parameters.AddWithValue("@EmpName", name)
cmd.Parameters.AddWithValue("@Designation", designation)
cmd.Parameters.AddWithValue("@GroupName", groupName)
cmd.Parameters.AddWithValue("@EmpId", id)
con.Open()
cmd.ExecuteNonQuery()
con.Close()
End Using
End Using
Me.GridView2.EditIndex = -1
Me.BindEmployees()
End Sub
Protected Sub GridView2_OnRowdeleting(ByVal sender As Object, ByVal e As GridViewDeleteEventArgs)
Dim row As GridViewRow = GridView2.Rows(e.RowIndex)
Dim id As String = (TryCast(row.FindControl("lblEmpID"), Label)).Text
Using con As SqlConnection = New SqlConnection(ConfigurationManager.ConnectionStrings("constr").ConnectionString)
Using cmd As SqlCommand = New SqlCommand("DELETE FROM Employee WHERE EmpId=@EmpId", con)
cmd.CommandType = CommandType.Text
cmd.Parameters.AddWithValue("@EmpId", id)
con.Open()
cmd.ExecuteNonQuery()
con.Close()
End Using
End Using
Me.BindEmployees()
End Sub
'------------- GridView2 End ------------
Private Sub BindCustomers()
Using con As SqlConnection = New SqlConnection(ConfigurationManager.ConnectionStrings("constr").ConnectionString)
Using cmd As SqlCommand = New SqlCommand("SELECT CustomerID,Name,Country FROM Customers", con)
cmd.CommandType = CommandType.Text
Using sda As SqlDataAdapter = New SqlDataAdapter()
sda.SelectCommand = cmd
Dim dt As DataTable = New DataTable()
sda.Fill(dt)
Me.GridView1.DataSource = dt
Me.GridView1.DataBind()
End Using
End Using
End Using
End Sub
Private Sub BindEmployees()
Using con As SqlConnection = New SqlConnection(ConfigurationManager.ConnectionStrings("constr").ConnectionString)
Using cmd As SqlCommand = New SqlCommand("SELECT EmpId,EmpName,Designation,GroupName FROM Employee", con)
cmd.CommandType = CommandType.Text
Using sda As SqlDataAdapter = New SqlDataAdapter()
sda.SelectCommand = cmd
Dim dt As DataTable = New DataTable()
sda.Fill(dt)
Me.GridView2.DataSource = dt
Me.GridView2.DataBind()
End Using
End Using
End Using
End Sub
Screenshot