When we click on "Add new row" button, we need to add one dynamic row which has two textbox and one dropdown fields within the datagrid. After entering the value, it will be displayed in the datagrid.
For that the source code is as below
<%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="TestDGrid.WebForm1" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" > <HTML> <HEAD> <title>WebForm1</title> <meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1"> <meta name="CODE_LANGUAGE" Content="C#"> <meta name="vs_defaultClientScript" content="JavaScript"> <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5"> </HEAD> <body MS_POSITIONING="GridLayout"> <form id="Form1" method="post" runat="server"> <asp:DataGrid id="DataGrid1" style="Z-INDEX: 101; LEFT: 112px; POSITION: absolute; TOP: 16px" runat="server" AutoGenerateColumns="False"> <Columns> <asp:TemplateColumn HeaderText="Id"> <ItemTemplate> <asp:TextBox id="txtID" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "iEid ") %>'> </asp:TextBox> </ItemTemplate> </asp:TemplateColumn> <asp:TemplateColumn HeaderText="Name"> <ItemTemplate> <asp:TextBox id="txtName" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "cEName") %>'> </asp:TextBox> </ItemTemplate> </asp:TemplateColumn> <asp:TemplateColumn HeaderText="Salary"> <ItemTemplate> <asp:TextBox id="txtSal" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "dSalary") %>'> </asp:TextBox> </ItemTemplate> </asp:TemplateColumn> </Columns> </asp:DataGrid> <asp:Button id="btAdd" style="Z-INDEX: 102; LEFT: 64px; POSITION: absolute; TOP: 24px" runat="server" Text="Add"></asp:Button> </form> </body> </HTML>
The code bedhind code is :
using System; using System.Collections; using System.ComponentModel; using System.Data; using System.Drawing; using System.Web; using System.Web.SessionState; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.HtmlControls; using System.Data.SqlClient; namespace TestDGrid { /// /// Summary description for WebForm1. /// public class WebForm1 : System.Web.UI.Page { protected System.Web.UI.WebControls.DataGrid DataGrid1; protected DataTable table = new DataTable(); protected System.Web.UI.WebControls.Button btAdd; SqlConnection con = new SqlConnection(@"Server=Servername;DataBase=database name;uid=sa;pwd=sa;"); private void Page_Load(object sender, System.EventArgs e) { // Put user code to initialize the page here if(!IsPostBack) { fill(); BindDG(); } }
public void fill() { SqlDataAdapter da = new SqlDataAdapter("Select * from emp",con); DataSet ds = new DataSet(); da.Fill(table); }
public void BindDG() { DataGrid1.DataSource=table; DataGrid1.DataBind(); }
public void Nrow() { table.Rows.InsertAt(table.NewRow(),0); }
#region Web Form Designer generated code override protected void OnInit(EventArgs e) { // // CODEGEN: This call is required by the ASP.NET Web Form Designer. // InitializeComponent(); base.OnInit(e); } /// /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// private void InitializeComponent() { this.btAdd.Click += new System.EventHandler(this.btAdd_Click); this.Load += new System.EventHandler(this.Page_Load);
} #endregion
private void btAdd_Click(object sender, System.EventArgs e) { DataGrid1.CurrentPageIndex = 0; fill(); Nrow(); BindDG(); } } }
|
| Author: Kapil Dhawan 17 Jun 2008 | Member Level: Gold Points : 2 |
Hello Nice piece of code Thanks for sharing your knowledge with us. I hope to see more good code from your side This code is going to help lots of guys. Ton Thanks to you Regards, Kapil
|