In the previous article I explained how to edit, delete and update the record in DataGrid. Now I am going to explain how to insert a new record in a DataGrid.
This is the aspx code.
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" Debug="true" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Edit Update Delete Cancel and Insert In DataGrid</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DataGrid ID="gridedit" runat="server" Width="100px"
DataKeyField="id" BorderStyle="Ridge" GridLines="None"
BorderWidth="2px" BorderColor="White" BackColor="White"
CellPadding="3" CellSpacing="1" AllowSorting="True"
PagerStyle-HorizontalAlign="Center" HorizontalAlign="Left"
OnEditCommand="editgrid_click" OnCancelCommand=
"gridcancel_click" OnPageIndexChanged=
"gridedit_PageIndexChanged"
OnUpdateCommand="updategrid_UpdateCommand"
Height="267px" PageSize=5 AllowPaging="true"
OnDeleteCommand="gridedit_DeleteCommand"
AutoGenerateColumns="false"
SelectedItemStyle-Width="100px">
<FooterStyle ForeColor="Black" BackColor="#C6C3C6"></FooterStyle>
<HeaderStyle Font-Bold="True" ForeColor="#FFFFFF" BackColor="#A53A6A"></HeaderStyle>
<FooterStyle BackColor="beige" />
<PagerStyle Font-Bold="true" Mode=NumericPages Font-Underline="true"/>
<Columns>
<asp:BoundColumn DataField=id HeaderText="ID" ItemStyle-Width="10px" Visible="false">
<ItemStyle BackColor="graytext" />
<HeaderStyle BackColor="graytext" />
</asp:BoundColumn>
<asp:BoundColumn DataField=name HeaderText="Name">
<ItemStyle BackColor=GhostWhite />
</asp:BoundColumn>
<asp:BoundColumn DataField=F_name HeaderText="F_Name">
<ItemStyle BackColor=GhostWhite />
</asp:BoundColumn>
<asp:BoundColumn DataField=l_name HeaderText="L_Name">
<ItemStyle BackColor=GhostWhite />
</asp:BoundColumn>
<asp:BoundColumn DataField=City HeaderText="City">
<ItemStyle BackColor=GhostWhite />
</asp:BoundColumn>
<asp:BoundColumn DataField=State HeaderText="State">
<ItemStyle BackColor=GhostWhite />
</asp:BoundColumn>
<asp:EditCommandColumn CancelText="Cancel" EditText="Edit" UpdateText="Update" HeaderText="Edit">
<ItemStyle BackColor=GhostWhite />
</asp:EditCommandColumn>
<asp:ButtonColumn CommandName="Delete" HeaderText="Delete" Text="Delete">
<ItemStyle BackColor=GhostWhite />
</asp:ButtonColumn>
</Columns>
</asp:DataGrid>
<br /><br />
<asp:Button ID="btninsert" runat="server" Text="Click Here to Insert a New record" Width="250px"
OnClick="InsertNewRecord_click" /><br /><br />
<asp:Label ID="lblnewname" runat="server" Width="120px" Text="Enter Name" Visible="false"></asp:Label>
<asp:TextBox ID="txtnewname" runat="server" Visible="false"></asp:TextBox><br /><br />
<asp:Label ID="lblF_name" runat="server" Width="120px" Text="Enter Father Name" Visible="false"></asp:Label>
<asp:TextBox ID="txtF_name" runat="server" Visible="false" ></asp:TextBox>
<br /><br />
<asp:Label ID="lblLast_name" runat="server" Width="120px" Text="Enter Last Name" Visible="false"></asp:Label>
<asp:TextBox ID="txtLast_Nmae" runat="server" Visible="false"></asp:TextBox>
<br /><br />
<asp:Label ID="lblcity" runat="server" Text="Enter city" Width="120px" Visible="false"></asp:Label>
<asp:TextBox ID="txtcity" runat="server" Visible="false"></asp:TextBox>
<br /><br />
<asp:Label ID="lblState" runat="server" Width="120px" Text="Enter State Name" Visible="false"></asp:Label>
<asp:TextBox ID="txtState" runat="server" Visible="false"></asp:TextBox>
<asp:Button ID="btnnewRecordSubmit" runat="server" Text="Insert" OnClick="Submitnew" Width="100px"
Visible="false" />
</div>
</form>
</body>
</html>
From this aspx code the application will look like as:

Figure 1: Application in Designing State.
I set here the visible property of alll label, textBox and insert button False. All of these wiil visible when I will click Insert a new record button.
The Source code for this is:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
SqlDataAdapter da;
DataSet ds = new DataSet();
SqlConnection con;
SqlCommand cmd = new SqlCommand();
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
Binddata();
}
}
// Define the Edit Command
public void editgrid_click(object sender, DataGridCommandEventArgs e)
{
gridedit.EditItemIndex = e.Item.ItemIndex;
Binddata();
}
// Define the Cancel Command
public void gridcancel_click(object sender, DataGridCommandEventArgs e)
{
gridedit.EditItemIndex = -1;
Binddata();
}
//Here we Bind the data
public void Binddata()
{
con = new SqlConnection(ConfigurationSettings.AppSettings["connect"]);
cmd.CommandText="select * from record order by id Asc";
cmd.Connection = con;
da = new SqlDataAdapter(cmd);
da.Fill(ds);
con.Open();
cmd.ExecuteNonQuery();
gridedit.DataSource = ds;
gridedit.DataBind();
con.Close();
}
//Update Command Defination
protected void updategrid_UpdateCommand(object source, DataGridCommandEventArgs e)
{
try
{
con = new SqlConnection(ConfigurationSettings.AppSettings["connect"]);
cmd.CommandText = "Update record set name=@name ,F_name=@F_Name, l_name=@l_name,City=@City,State=@State where id=@id";
cmd.Parameters.Add("@name", SqlDbType.Char).Value = ((TextBox)e.Item.Cells[1].Controls[0]).Text;
cmd.Parameters.Add("@F_name", SqlDbType.Char).Value = ((TextBox)e.Item.Cells[2].Controls[0]).Text;
cmd.Parameters.Add("@l_name", SqlDbType.Char).Value = ((TextBox)e.Item.Cells[3].Controls[0]).Text;
cmd.Parameters.Add("@City", SqlDbType.Char).Value = ((TextBox)e.Item.Cells[4].Controls[0]).Text;
cmd.Parameters.Add("@State", SqlDbType.Char).Value = ((TextBox)e.Item.Cells[5].Controls[0]).Text;
cmd.Parameters.Add("@id", SqlDbType.Int).Value = gridedit.DataKeys[e.Item.ItemIndex];
cmd.Connection = con;
cmd.Connection.Open();
cmd.ExecuteNonQuery();
cmd.Connection.Close();
gridedit.EditItemIndex = -1;
Binddata();
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
}
// Delete Command Defination
public void gridedit_DeleteCommand(object sender, DataGridCommandEventArgs e)
{
con = new SqlConnection(ConfigurationSettings.AppSettings["connect"]);
int U_ID = (int)gridedit.DataKeys[(int)e.Item.ItemIndex];
cmd.CommandText = " Delete from record where id=" + U_ID;
cmd.Connection = con;
cmd.Connection.Open();
cmd.ExecuteNonQuery();
cmd.ExecuteNonQuery();
cmd.Connection.Close();
gridedit.EditItemIndex = -1;
Binddata();
}
// For Paging
public void gridedit_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
{
gridedit.CurrentPageIndex = e.NewPageIndex;
Binddata();
}
//Link for Insert a new Record in a table
public void InsertNewRecord_click(object source, System.EventArgs e)
{
lblnewname.Visible = true;
txtnewname.Visible = true;
lblF_name.Visible = true;
txtF_name.Visible = true;
lblLast_name.Visible = true;
txtLast_Nmae.Visible = true;
lblcity.Visible = true;
txtcity.Visible = true;
lblState.Visible = true;
txtState.Visible = true;
btnnewRecordSubmit.Visible = true;
}
// Command for insert a new Record
public void Submitnew(object source, System.EventArgs e)
{
con = new SqlConnection(ConfigurationSettings.AppSettings["connect"]);
cmd.CommandText = "insert into record(name,F_name,l_name,City,State) values(@name,@F_Name,@l_name,@City,@State)";
cmd.Parameters.Add("@name", SqlDbType.Char).Value = txtnewname.Text;
cmd.Parameters.Add("@F_Name", SqlDbType.Char).Value = txtF_name.Text;
cmd.Parameters.Add("@l_name", SqlDbType.Char).Value = txtLast_Nmae.Text;
cmd.Parameters.Add("@City", SqlDbType.Char).Value = txtcity.Text;
cmd.Parameters.Add("@State", SqlDbType.Char).Value = txtState.Text;
cmd.Connection = con;
cmd.Connection.Open();
cmd.ExecuteNonQuery();
cmd.Connection.Close();
gridedit.EditItemIndex = -1;
Binddata();
txtnewname.Text = "";
txtF_name.Text = "";
txtLast_Nmae.Text = "";
txtcity.Text = "";
txtState.Text = "";
lblnewname.Visible = false;
txtnewname.Visible = false;
lblF_name.Visible = false;
txtF_name.Visible = false;
lblLast_name.Visible = false;
txtLast_Nmae.Visible = false;
lblcity.Visible = false;
txtcity.Visible = false;
lblState.Visible = false;
txtState.Visible = false;
btnnewRecordSubmit.Visible = false;
}
}
Now when User will run the project then the window will look like as:
Figure 2: In running State Project.
Suppose, if user wants to insert a new record, then they have to click on "Click Here" button to insert a new record. Onclick of the button, the new window will look like this:

Figure 3: Insert a new Record.
When User click this insert button then the record will insert in the DataGrid.

Figure 4: Record Inserted.