....................................................................
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="DynamciGridView.aspx.cs" Inherits="DynamciGridView" %>
<!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></title>
</head>
<body>
<form id="form1" runat="server">
<div style="height: 55px"><table align="center" ><tr><td>
<asp:PlaceHolder ID="PlaceHolder1" runat="server" />
</td></tr>
<tr><td><center ><asp:Button ID="btnSaveGrid" runat="server" Text="Save"
Visible="false" BackColor="#006666" BorderColor="#CC00CC" Font-Bold="True"
Font-Italic="True" Font-Size="Medium" ForeColor="#FF33CC" Height="26px"
onclick="btnSaveGrid_Click" Width="51px" /></center>
</td></tr></table>
</div>
</form>
</body>
</html>
...........................................................................................................................
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web.Security;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls.WebParts;
public partial class DynamciGridView : System.Web.UI.Page
{
GridView gvDynamic = new GridView();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
FirstGridViewRow();
gvDynamic.AutoGenerateColumns = false;
gvDynamic.HeaderStyle.BackColor = System.Drawing.Color.DarkTurquoise;
gvDynamic.RowStyle.BackColor = System.Drawing.Color.DarkMagenta;
gvDynamic.AlternatingRowStyle.BackColor = System.Drawing.Color.DarkSalmon;
}
}
private void FirstGridViewRow()
{
DataTable dt = new DataTable();
TextBox txtname = new TextBox();
DataRow dr = null;
DataRow dr2 = null;
dr = dt.NewRow();
dr2 = dt.NewRow();
dt.Rows.Add(dr);
dt.Rows.Add(dr2);
TemplateField tfObject = new TemplateField();
tfObject.HeaderText = "Name";
//tfObject.HeaderStyle.Width = Unit.Percentage(30);
tfObject.ItemTemplate = new CreateItemTemplate(ListItemType.Item);
gvDynamic.Columns .Add (tfObject);
//TemplateField tfObject2 = new TemplateField();
//tfObject2.HeaderText = "City";
////tfObject2.HeaderStyle.Width = Unit.Percentage(30);
//tfObject2.ItemTemplate = new CreateItemTemplate(ListItemType.Item);
//gvDynamic.Columns.Add(tfObject2);
//TemplateField tfObject3 = new TemplateField();
//tfObject3.HeaderText = "Cont. No.";
////tfObject2.HeaderStyle.Width = Unit.Percentage(30);
//tfObject3.ItemTemplate = new CreateItemTemplate(ListItemType.Item);
//gvDynamic.Columns.Add(tfObject3);
tfObject = new TemplateField();
tfObject.HeaderText = "City";
tfObject.ItemTemplate = new CreateItemTemplate(ListItemType.Item);
gvDynamic.Columns.Add(tfObject);
tfObject = new TemplateField();
tfObject.HeaderText = "Cont. No.";
tfObject.ItemTemplate = new CreateItemTemplate(ListItemType.Item);
gvDynamic.Columns.Add(tfObject);
//tfObject.HeaderTemplate = new CreateItemTemplate(ListItemType.Header);
//tfObject.FooterTemplate = new CreateItemTemplate(ListItemType.Footer);
ViewState["CurrentTable"] = dt;
gvDynamic.DataSource = dt;
gvDynamic.DataBind();
PlaceHolder1.Controls.Add(gvDynamic);
btnSaveGrid.Visible = true;
}
protected void btnSaveGrid_Click(object sender, EventArgs e)
{
}
public class CreateItemTemplate : ITemplate
{
//Field to store the ListItemType value
private ListItemType myListItemType;
public CreateItemTemplate()
{
//
// TODO: Add default constructor logic here
//
}
//Parameterrised constructor
public CreateItemTemplate(ListItemType Item)
{
myListItemType = Item;
}
//Overwrite the InstantiateIn() function of the ITemplate interface.
public void InstantiateIn(System.Web.UI.Control container)
{
//Code to create the ItemTemplate and its field.
if (myListItemType == ListItemType.Item)
{
TextBox txtCashCheque = new TextBox();
txtCashCheque.ID = "txtDynamic";
container.Controls.Add(txtCashCheque);
}
//Code to create the HeaderTemplate and its field.
if (myListItemType == ListItemType.Header)
{
Label lblTest = new Label();
lblTest.Text = "Header Text";
container.Controls.Add(lblTest);
}
//Code to create the FooterTemplate and its field.
if (myListItemType == ListItemType.Footer)
{
Label lblTest = new Label();
lblTest.Text = "";
container.Controls.Add(lblTest);
}
}
}
Just Paste This Code.
All The Best!!!
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="DynamciGridView.aspx.cs" Inherits="DynamciGridView" %>
<!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></title>
</head>
<body>
<form id="form1" runat="server">
<div style="height: 55px"><table align="center" ><tr><td>
<asp:PlaceHolder ID="PlaceHolder1" runat="server" />
</td></tr>
<tr><td><center ><asp:Button ID="btnSaveGrid" runat="server" Text="Save"
Visible="false" BackColor="#006666" BorderColor="#CC00CC" Font-Bold="True"
Font-Italic="True" Font-Size="Medium" ForeColor="#FF33CC" Height="26px"
onclick="btnSaveGrid_Click" Width="51px" /></center>
</td></tr></table>
</div>
</form>
</body>
</html>
...........................................................................................................................
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web.Security;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls.WebParts;
public partial class DynamciGridView : System.Web.UI.Page
{
GridView gvDynamic = new GridView();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
FirstGridViewRow();
gvDynamic.AutoGenerateColumns = false;
gvDynamic.HeaderStyle.BackColor = System.Drawing.Color.DarkTurquoise;
gvDynamic.RowStyle.BackColor = System.Drawing.Color.DarkMagenta;
gvDynamic.AlternatingRowStyle.BackColor = System.Drawing.Color.DarkSalmon;
}
}
private void FirstGridViewRow()
{
DataTable dt = new DataTable();
TextBox txtname = new TextBox();
DataRow dr = null;
DataRow dr2 = null;
dr = dt.NewRow();
dr2 = dt.NewRow();
dt.Rows.Add(dr);
dt.Rows.Add(dr2);
TemplateField tfObject = new TemplateField();
tfObject.HeaderText = "Name";
//tfObject.HeaderStyle.Width = Unit.Percentage(30);
tfObject.ItemTemplate = new CreateItemTemplate(ListItemType.Item);
gvDynamic.Columns .Add (tfObject);
//TemplateField tfObject2 = new TemplateField();
//tfObject2.HeaderText = "City";
////tfObject2.HeaderStyle.Width = Unit.Percentage(30);
//tfObject2.ItemTemplate = new CreateItemTemplate(ListItemType.Item);
//gvDynamic.Columns.Add(tfObject2);
//TemplateField tfObject3 = new TemplateField();
//tfObject3.HeaderText = "Cont. No.";
////tfObject2.HeaderStyle.Width = Unit.Percentage(30);
//tfObject3.ItemTemplate = new CreateItemTemplate(ListItemType.Item);
//gvDynamic.Columns.Add(tfObject3);
tfObject = new TemplateField();
tfObject.HeaderText = "City";
tfObject.ItemTemplate = new CreateItemTemplate(ListItemType.Item);
gvDynamic.Columns.Add(tfObject);
tfObject = new TemplateField();
tfObject.HeaderText = "Cont. No.";
tfObject.ItemTemplate = new CreateItemTemplate(ListItemType.Item);
gvDynamic.Columns.Add(tfObject);
//tfObject.HeaderTemplate = new CreateItemTemplate(ListItemType.Header);
//tfObject.FooterTemplate = new CreateItemTemplate(ListItemType.Footer);
ViewState["CurrentTable"] = dt;
gvDynamic.DataSource = dt;
gvDynamic.DataBind();
PlaceHolder1.Controls.Add(gvDynamic);
btnSaveGrid.Visible = true;
}
protected void btnSaveGrid_Click(object sender, EventArgs e)
{
}
public class CreateItemTemplate : ITemplate
{
//Field to store the ListItemType value
private ListItemType myListItemType;
public CreateItemTemplate()
{
//
// TODO: Add default constructor logic here
//
}
//Parameterrised constructor
public CreateItemTemplate(ListItemType Item)
{
myListItemType = Item;
}
//Overwrite the InstantiateIn() function of the ITemplate interface.
public void InstantiateIn(System.Web.UI.Control container)
{
//Code to create the ItemTemplate and its field.
if (myListItemType == ListItemType.Item)
{
TextBox txtCashCheque = new TextBox();
txtCashCheque.ID = "txtDynamic";
container.Controls.Add(txtCashCheque);
}
//Code to create the HeaderTemplate and its field.
if (myListItemType == ListItemType.Header)
{
Label lblTest = new Label();
lblTest.Text = "Header Text";
container.Controls.Add(lblTest);
}
//Code to create the FooterTemplate and its field.
if (myListItemType == ListItemType.Footer)
{
Label lblTest = new Label();
lblTest.Text = "";
container.Controls.Add(lblTest);
}
}
}
Just Paste This Code.
All The Best!!!
No comments:
Post a Comment