Example of DataGrid in ASP.NET

This is the aspx code in DataGrid

From Here we can design our web form. We use a DataGrid on webForm. Here we set all the property as we have to perform the operation with in DataGrid.

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="DataGrid.aspx.cs" Inherits="sapnamalik_DataGrid" %>  
  2.     <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  3.     <html xmlns="http://www.w3.org/1999/xhtml">  
  4.   
  5.     <head runat="server">  
  6.         <title>Untitled Page</title>  
  7.     </head>  
  8.   
  9.     <body>  
  10.         <form id="form1" runat="server">  
  11.             <div>  
  12.                 <asp:DataGrid ID="Grid" runat="server" PageSize="5" AllowPaging="True" DataKeyField="EmpId" AutoGenerateColumns="False" CellPadding="4" ForeColor="#333333" GridLines="None" OnPageIndexChanged="Grid_PageIndexChanged" OnCancelCommand="Grid_CancelCommand" OnDeleteCommand="Grid_DeleteCommand" OnEditCommand="Grid_EditCommand" OnUpdateCommand="Grid_UpdateCommand">  
  13.                     <Columns>  
  14.                         <asp:BoundColumn HeaderText="EmpId" DataField="EmpId"> </asp:BoundColumn>  
  15.                         <asp:BoundColumn HeaderText="F_Name" DataField="F_Name"> </asp:BoundColumn>  
  16.                         <asp:BoundColumn HeaderText="L_Name" DataField="L_Name"> </asp:BoundColumn>  
  17.                         <asp:BoundColumn DataField="City" HeaderText="City"> </asp:BoundColumn>  
  18.                         <asp:BoundColumn DataField="EmailId" HeaderText="EmailId"> </asp:BoundColumn>  
  19.                         <asp:BoundColumn DataField="EmpJoining" HeaderText="EmpJoining"> </asp:BoundColumn>  
  20.                         <asp:EditCommandColumn EditText="Edit" CancelText="Cancel" UpdateText="Update" HeaderText="Edit"> </asp:EditCommandColumn>  
  21.                         <asp:ButtonColumn CommandName="Delete" HeaderText="Delete" Text="Delete"> </asp:ButtonColumn>  
  22.                     </Columns>  
  23.                     <FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />  
  24.                     <SelectedItemStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />  
  25.                     <PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" Mode="NumericPages" />  
  26.                     <AlternatingItemStyle BackColor="White" />  
  27.                     <ItemStyle BackColor="#FFFBD6" ForeColor="#333333" />  
  28.                     <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" /> </asp:DataGrid> <br /> <br />  
  29.                 <table>  
  30.                     <tr>  
  31.                         <td>  
  32.                             <asp:Label ID="lblEmpId" runat="server" Text="EmpId"></asp:Label>  
  33.                             <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>  
  34.                         </td>  
  35.                         <td>  
  36.                             <asp:Label ID="lblfname" runat="server" Text="F_Name"></asp:Label>  
  37.                             <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>  
  38.                         </td>  
  39.                         <td>  
  40.                             <asp:Label ID="lblLname" runat="server" Text="L_Name"></asp:Label>  
  41.                             <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>  
  42.                         </td>  
  43.                         <td>  
  44.                             <asp:Label ID="lblCity" runat="server" Text="City"></asp:Label>  
  45.                             <asp:TextBox ID="TextBox4" runat="server"></asp:TextBox>  
  46.                         </td>  
  47.                         <td>  
  48.                             <asp:Label ID="lblEmailId" runat="server" Text="EmailId"></asp:Label>  
  49.                             <asp:TextBox ID="TextBox5" runat="server"></asp:TextBox> Q </td>  
  50.                         <td>  
  51.                             <asp:Label ID="lblEmpJoining" runat="server" Text="EmpJoining"></asp:Label>  
  52.                             <asp:TextBox ID="TextBox6" runat="server"></asp:TextBox>  
  53.                         </td>  
  54.                     </tr>  
  55.                 </table>  
  56.                 <asp:Button ID="btnsubmit" runat="server" Text="Submit" OnClick="btnsubmit_Click" />  
  57.                 <asp:Button ID="btnReset" runat="server" Text="Reset" OnClick="btnReset_Click" />  
  58.                 <asp:Button ID="btnOk" runat="server" Text="OK" OnClick="btnOk_Click" /> </div>  
  59.             <div>  
  60.                 <asp:DataGrid ID="Grid1" runat="server" PageSize="5" AllowPaging="True" AutoGenerateColumns="False" CellPadding="4" ForeColor="#333333" GridLines="None">  
  61.                     <Columns>  
  62.                         <asp:BoundColumn HeaderText="EmpId" DataField="EmpId"></asp:BoundColumn>  
  63.                         <asp:BoundColumn HeaderText="F_Name" DataField="F_Name"></asp:BoundColumn>  
  64.                         <asp:BoundColumn HeaderText="L_Name" DataField="L_Name"></asp:BoundColumn>  
  65.                         <asp:BoundColumn DataField="City" HeaderText="City"></asp:BoundColumn>  
  66.                         <asp:BoundColumn DataField="EmailId" HeaderText="EmailId"></asp:BoundColumn>  
  67.                         <asp:BoundColumn DataField="EmpJoining" HeaderText="EmpJoining"> </asp:BoundColumn>  
  68.                     </Columns>  
  69.                     <FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />  
  70.                     <SelectedItemStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />  
  71.                     <PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" Mode="NumericPages" />  
  72.                     <AlternatingItemStyle BackColor="White" />  
  73.                     <ItemStyle BackColor="#FFFBD6" ForeColor="#333333" />  
  74.                     <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" /> </asp:DataGrid>  
  75.             </div>  
  76.         </form>  
  77.     </body>  
  78.   
  79.     </html>  

This is .cs code in DataGrid,

  1. using System;  
  2. using System.Data;  
  3. using System.Configuration;  
  4. using System.Collections;  
  5. using System.Web;  
  6. using System.Web.Security;  
  7. using System.Web.UI;  
  8. using System.Web.UI.WebControls;  
  9. using System.Web.UI.WebControls.WebParts;  
  10. using System.Web.UI.HtmlControls;  
  11. using System.Data.SqlClient;  
  12. public partial class sapnamalik_DataGrid: System.Web.UI.Page {  
  13.     SqlDataAdapter da;  
  14.     DataSet ds = new DataSet();  
  15.     SqlCommand cmd = new SqlCommand();  
  16.     SqlConnection con;  
  17.     protected void Page_Load(object sender, EventArgs e) {  
  18.         if (!Page.IsPostBack) {  
  19.             BindData();  
  20.         }  
  21.     }  
  22.     public void BindData() {  
  23.         con = new SqlConnection(ConfigurationManager.AppSettings["connect"]);  
  24.         cmd.CommandText = "Select * from Employee";  
  25.         cmd.Connection = con;  
  26.         da = new SqlDataAdapter(cmd);  
  27.         da.Fill(ds);  
  28.         con.Open();  
  29.         cmd.ExecuteNonQuery();  
  30.         Grid.DataSource = ds;  
  31.         Grid.DataBind();  
  32.         con.Close();  
  33.     }  
  34.     protected void Grid_PageIndexChanged(object source, DataGridPageChangedEventArgs e) {  
  35.         Grid.CurrentPageIndex = e.NewPageIndex;  
  36.         BindData();  
  37.     }  
  38.     protected void Grid_EditCommand(object source, DataGridCommandEventArgs e) {  
  39.         Grid.EditItemIndex = e.Item.ItemIndex;  
  40.         BindData();  
  41.     }  
  42.     protected void Grid_CancelCommand(object source, DataGridCommandEventArgs e) {  
  43.         Grid.EditItemIndex = -1;  
  44.         BindData();  
  45.     }  
  46.     protected void Grid_DeleteCommand(object source, DataGridCommandEventArgs e) {  
  47.         con = new SqlConnection(ConfigurationManager.AppSettings["connect"]);  
  48.         cmd.Connection = con;  
  49.         int EmpId = (int) Grid.DataKeys[(int) e.Item.ItemIndex];  
  50.         cmd.CommandText = "Delete from Employee where EmpId=" + EmpId;  
  51.         cmd.Connection.Open();  
  52.         cmd.ExecuteNonQuery();  
  53.         cmd.Connection.Close();  
  54.         Grid.EditItemIndex = -1;  
  55.         BindData();  
  56.     }  
  57.     protected void Grid_UpdateCommand(object source, DataGridCommandEventArgs e) {  
  58.         con = new SqlConnection(ConfigurationManager.AppSettings["connect"]);  
  59.         cmd.Parameters.Add("@EmpId", SqlDbType.Int).Value = ((TextBox) e.Item.Cells[0].Controls[0]).Text;  
  60.         cmd.Parameters.Add("@F_Name", SqlDbType.Char).Value = ((TextBox) e.Item.Cells[1].Controls[0]).Text;  
  61.         cmd.Parameters.Add("@L_Name", SqlDbType.Char).Value = ((TextBox) e.Item.Cells[2].Controls[0]).Text;  
  62.         cmd.Parameters.Add("@City", SqlDbType.Char).Value = ((TextBox) e.Item.Cells[3].Controls[0]).Text;  
  63.         cmd.Parameters.Add("@EmailId", SqlDbType.Char).Value = ((TextBox) e.Item.Cells[4].Controls[0]).Text;  
  64.         cmd.Parameters.Add("@EmpJoining", SqlDbType.DateTime).Value = DateTime.Now.ToString();  
  65.         cmd.CommandText = "Update Employee set F_Name=@F_Name,L_Name=@L_Name,City=@City,EmailId=@EmailId,EmpJoining=@EmpJoining where EmpId=@EmpId";  
  66.         cmd.Connection = con;  
  67.         cmd.Connection.Open();  
  68.         cmd.ExecuteNonQuery();  
  69.         cmd.Connection.Close();  
  70.         Grid.EditItemIndex = -1;  
  71.         BindData();  
  72.     }  
  73.     protected void btnsubmit_Click(object sender, EventArgs e) {  
  74.         SqlConnection con;  
  75.         con = new SqlConnection(ConfigurationManager.AppSettings["connect"]);  
  76.         con.Open();  
  77.         SqlCommand cmd;  
  78.         cmd = new SqlCommand("Insert into Employee (EmpId,F_Name,L_Name,City,EmailId,EmpJoining) values('" + TextBox1.Text + "','" + TextBox2.Text + "','" + TextBox3.Text + "','" + TextBox4.Text + "','" + TextBox5.Text + "','" + TextBox6.Text + "')", con);  
  79.         cmd.ExecuteNonQuery();  
  80.         con.Close();  
  81.     }  
  82.     protected void btnReset_Click(object sender, EventArgs e) {  
  83.         TextBox1.Text = "";  
  84.         TextBox2.Text = "";  
  85.         TextBox3.Text = "";  
  86.         TextBox4.Text = "";  
  87.         TextBox5.Text = "";  
  88.         TextBox6.Text = "";  
  89.     }  
  90.     protected void btnOk_Click(object sender, EventArgs e) {  
  91.         BindData1();  
  92.     }  
  93.     public void BindData1() {  
  94.         con = new SqlConnection(ConfigurationManager.AppSettings["connect"]);  
  95.         cmd.CommandText = "Select * from Employee";  
  96.         cmd.Connection = con;  
  97.         da = new SqlDataAdapter(cmd);  
  98.         da.Fill(ds);  
  99.         con.Open();  
  100.         cmd.ExecuteNonQuery();  
  101.         Grid1.DataSource = ds;  
  102.         Grid1.DataBind();  
  103.         con.Close();  
  104.     }  
  105. }  

Now when User will run the project then the window will look like as,

datagrid1.gif  

Click the ok button and we can show the data at the same page.

datagrid2.gif

 
datagrid3.gif


Similar Articles