First Time Page Load Gridview Shown as below

first_time_gridview

.aspx Page Coding

<div>
<asp:GridView ID=”GridView1″ runat=”server” AutoGenerateColumns=”False” ShowFooter=”True” >
<Columns>
<asp:TemplateField HeaderText=”Rollno”>
<ItemTemplate>
<asp:Label ID=”lblRno” runat=”Server” Text=””></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText=”Name”>
<ItemTemplate>
<asp:Label ID=”lblName” runat=”server” Text='<%# Eval(“name”) %>’></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID=”txtName” runat=”server” Text=””></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText=”City”>
<ItemTemplate>
<asp:Label ID=”lblCity” runat=”server” Text='<%# Eval(“city”) %>’></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID=”txtCity” runat=”server” Text=””></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate>
<asp:Button ID=”btnAdd” runat=”server” Text=”ADD” OnClick=”btnAdd_Click” />
</HeaderTemplate>
</asp:TemplateField>
</Columns>
<EmptyDataTemplate>
No Records
</EmptyDataTemplate>
</asp:GridView>
<asp:Button ID=”Button1″ runat=”server” OnClick=”Button1_Click” Style=”z-index: 100;
left: 16px; position: absolute; top: 192px” Text=”Insert Total Records from Grdiview” />
</div>

.aspx.cs Coding

DataTable dt = new DataTable();
DataColumn dc;
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
Session[“cart”] = null;
MakeCart();
BindGrid();
}
}

//Define columns of DataTable
public void MakeCart()
{
dc = new DataColumn();
dc.ColumnName = “rollno”;
dc.DataType = typeof(int);
dt.Columns.Add(dc);

dc = new DataColumn();
dc.ColumnName = “name”;
dc.DataType = typeof(string);
dt.Columns.Add(dc);

dc = new DataColumn();
dc.ColumnName = “city”;
dc.DataType = typeof(string);
dt.Columns.Add(dc);

}

//Bind Records into Gridview
public void BindGrid()
{
if (dt.Rows.Count == 0)
{
DataRow dr = dt.NewRow();

dr[“name”] = “No Record”;
dr[“city”] = “No Record”;
dt.Rows.Add(dr);
}
GridView1.DataSource = dt;
GridView1.DataBind();
}
protected void btnAdd_Click(object sender, EventArgs e)
{
if (Session[“cart”] == null)
{
MakeCart();
DataRow dr = dt.NewRow();
dr[“name”] = ((TextBox)GridView1.FooterRow.FindControl(“txtName”)).Text;
dr[“city”] = ((TextBox)GridView1.FooterRow.FindControl(“txtCity”)).Text;
dt.Rows.Add(dr);
Session[“cart”] = dt;
BindGrid();
}
else
{
dt = (DataTable)Session[“cart”];
DataRow dr = dt.NewRow();
dr[“name”] = ((TextBox)GridView1.FooterRow.FindControl(“txtName”)).Text;
dr[“city”] = ((TextBox)GridView1.FooterRow.FindControl(“txtCity”)).Text;
dt.Rows.Add(dr);
Session[“cart”] = dt;
BindGrid();
}
}

//Insert All Record from Gridview to DataSource  Sqlserver table.
protected void Button1_Click(object sender, EventArgs e)
{
dt = (DataTable)Session[“cart”];
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings[“TESTConnectionString”].ToString());
conn.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = “insTy”;
for (int i=0; i <dt.Rows.Count; i++)
{
cmd.Parameters.AddWithValue(“@nm”, dt.Rows[i][“name”]);
cmd.Parameters.AddWithValue(“@ct”, dt.Rows[i][“city”]);
cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
}
}

Advertisements