CODE:CREATING DYNAMIC EDITABLE TABLE FROM DATABASE IN DOTNET

Default.aspx code:

<form id="form1" runat="server">
<div style="width:80%;margin:auto">
<div class="panel panel-default" style="border-color: #a8ddeb; ">
<div class="panel-heading" style="background-color:#dbedf2"><b>Canteen Home</b></div>
<div class="panel-body" style="font-family:Verdana;font-size:11px;">
<br />
<br />
<div>
<asp:GridView CssClass="grid" ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="6" OnRowCancelingEdit="GridView1_RowCancelingEdit"
OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating" Width="100%">
<Columns>
<asp:TemplateField HeaderText="Update" HeaderStyle-Height="40">
<ItemStyle HorizontalAlign="Center" />
<ItemTemplate>
<asp:Button text-align="center" Height="30" class="btn btn-primary" ID="btn_Edit" runat="server" Text="Edit" CommandName="Edit" />
</ItemTemplate>
<EditItemTemplate>
<asp:Button Height="30" class="btn btn-primary" ID="btn_Update" runat="server" Text="Update" CommandName="Update"/>
<asp:Button Height="30" class="btn btn-primary" ID="btn_Cancel" runat="server" Text="Cancel" CommandName="Cancel"/>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="ID">
<ItemStyle HorizontalAlign="Center" />
<ItemTemplate >
<b> <asp:Label Height="40" ID="ID" runat="server" Text='<%#Eval("id") %>'></asp:Label></b>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Food">
<ItemStyle HorizontalAlign="Center" />
<ItemTemplate>
<b> <asp:Label ID="FOOD" runat="server" Text='<%#Eval("Food") %>'></asp:Label></b>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txt_FOOD" runat="server" Text='<%#Eval("Food") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Cost">
<ItemStyle HorizontalAlign="Center" />
<ItemTemplate>
<b><asp:Label ID="COST" runat="server" Text='<%#Eval("Cost") %>'></asp:Label></b>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txt_COST" runat="server" Text='<%#Eval("Cost") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Status">
<ItemStyle HorizontalAlign="Center" />
<ItemTemplate>
<asp:Label ID="status" runat="server" Text='<%#Eval("status") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:RadioButton ID="status_yes" runat="server" GroupName="Availability" Text="Yes" />
<asp:RadioButton ID="status_no" runat="server" GroupName="Availability" Text="No" />
</EditItemTemplate>
</asp:TemplateField>
</Columns>
<HeaderStyle BackColor="#76B4AE" ForeColor="#ffffff"/>
<RowStyle BackColor="#ffffff"/>
<FooterStyle BackColor="#666762" ForeColor="#ffffff" Height="30" Font-Bold="true"/>
</asp:GridView>
<style type="text/css">
.grid td, .grid th{
text-align:center;
}
</div>
</div>
</div>
</div>
</form>
C# Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
namespace CANTEEN
{
public partial class CanteenHome : System.Web.UI.Page
{
//Connection String from web.config File
CodeClass Ws = new CodeClass();
SqlDataAdapter adapt;
DataTable dt;
protected void Page_Load(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(Ws.Con);
if (!IsPostBack)
{
ShowData();
}
}
//ShowData method for Displaying Data in Gridview
protected void ShowData()
{
dt = new DataTable();
SqlConnection con = new SqlConnection(Ws.Con);
con.Open();
adapt = new SqlDataAdapter("Select id,Food,Cost,status from OrderFood", con);
adapt.Fill(dt);
if (dt.Rows.Count > 0)
{
GridView1.DataSource = dt;
GridView1.DataBind();
}
con.Close();
}
protected void GridView1_RowEditing(object sender, System.Web.UI.WebControls.GridViewEditEventArgs e)
{
//NewEditIndex property used to determine the index of the row being edited.
GridView1.EditIndex = e.NewEditIndex;
ShowData();
}
protected void GridView1_RowUpdating(object sender, System.Web.UI.WebControls.GridViewUpdateEventArgs e)
{
//Finding the controls from Gridview for the row which is going to update
Label id = GridView1.Rows[e.RowIndex].FindControl("ID") as Label;
TextBox name = GridView1.Rows[e.RowIndex].FindControl("txt_FOOD") as TextBox;
TextBox city = GridView1.Rows[e.RowIndex].FindControl("txt_COST") as TextBox;
RadioButton statusyes = GridView1.Rows[e.RowIndex].FindControl("status_yes") as RadioButton;
RadioButton statusno = GridView1.Rows[e.RowIndex].FindControl("status_no") as RadioButton;
if (statusyes.Checked == true)
{
statusyes.Text = "Yes";
}
else if (statusno.Checked == true)
{
statusyes.Text = "No";
}
else
{
statusyes.Text = "";
}
SqlConnection con = new SqlConnection(Ws.Con);
con.Open();
//updating the record
SqlCommand cmd = new SqlCommand("Update OrderFood set status='" + statusyes.Text + "',Food='" + name.Text + "',Cost='" + city.Text + "' where id=" + Convert.ToInt32(id.Text), con);
cmd.ExecuteNonQuery();
con.Close();
//Setting the EditIndex property to -1 to cancel the Edit mode in Gridview
GridView1.EditIndex = -1;
//Call ShowData method for displaying updated data
ShowData();
}
protected void GridView1_RowCancelingEdit(object sender, System.Web.UI.WebControls.GridViewCancelEditEventArgs e)
{
//Setting the EditIndex property to -1 to cancel the Edit mode in Gridview
GridView1.EditIndex = -1;
ShowData();
}
}
}

Leave a Reply

Your email address will not be published. Required fields are marked *