|
|||||||||||||||||||||
|
|||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
IntroductionHere is a code where you can create a Creating a grid dynamically with bound columns and even with Button column is simple, but with Template columns, it is quite complicated. Here is a clean solution for the same. :)
Creating a Grid and declaring columns:dgRt.ID="dgRt1";//Grid Settings
dgRt.AutoGenerateColumns=true;
dgRt.AllowPaging=true;
dgRt.ShowFooter=true;
dgRt.ShowHeader=true;
declare Columns:
BoundColumn bcl1 = new BoundColumn();
//columns Decalration
TemplateColumn tcl1 = new TemplateColumn();
TemplateColumn tcl2 = new TemplateColumn();
TemplateColumn tcl3 = new TemplateColumn();
TemplateColumn tcl4 = new TemplateColumn();
TemplateColumn tcl5 = new TemplateColumn();
TemplateColumn tcl6 = new TemplateColumn();
TemplateColumn tcl7 = new TemplateColumn();
TemplateColumn tcl8 = new TemplateColumn();
TemplateColumn tcl9 = new TemplateColumn();
TemplateColumn tcl10 = new TemplateColumn();
TemplateColumn tcl11 = new TemplateColumn();
EditCommandColumn ecol1=new EditCommandColumn();
Class File Inherited from ITemplate for Creating Templates:You need to write a separate class file which inherits the Here, I am writing a code to create an public class CreateItemTemplateDDL : ITemplate
{
DataTable dtBind;
string strddlName;
string strDataValueField;
string strDataTextField;
Here, I added the constructor for creating the DDL with value member and text member. public CreateItemTemplateDDL(string DDLName,
string DataValueField, string DataTextField, DataTable DDLSource)
{
this.dtBind=DDLSource;
this.strDataValueField=DataValueField;
this.strDataTextField=DataTextField;
this.strddlName=DDLName;
}
Here is the code for declaring public void InstantiateIn(Control objContainer)
{
DropDownList ddl = new DropDownList();
ddl.DataBinding+=new EventHandler(ddl_DataBinding);
objContainer.Controls.Add(ddl);
}
Here, you can assign all the properties and every thing else required for the DDL. private void ddl_DataBinding(object sender, EventArgs e)
{
DropDownList ddl= (DropDownList)sender;
ddl.ID=strddlName;
ddl.DataSource=dtBind;
ddl.DataValueField=strDataValueField;
ddl.DataTextField=strDataTextField;
//ddl.DataBind();
}
More control classes are available in the download. Likewise, you can add more controls to your For using this class: tcl7.ItemTemplate= new CreateItemTemplateDDL("ddlGrid","ID","UserName",dt);
Here, you have added the DDL for the Add all Columns to the Grid:dgRt.Columns.Add(tcl1);
dgRt.Columns.Add(tcl2);
dgRt.Columns.Add(tcl3);
dgRt.Columns.Add(tcl4);
dgRt.Columns.Add(tcl5);
dgRt.Columns.Add(tcl6);
dgRt.Columns.Add(tcl7);
dgRt.Columns.Add(tcl8);
dgRt.Columns.Add(tcl9);
dgRt.Columns.Add(tcl11);
dgRt.Columns.Add(ecol1);
dgRt.Columns.Add(bcl1);
Here by, you have added all the columns to the grid. Create Events:After creating grid, if you wish to have some events for it, say you want to bind the grid, then you need to create a dgRt.ItemDataBound+=new DataGridItemEventHandler(dgRt_ItemDataBound); dgRt.ItemCommand+=new DataGridCommandEventHandler(dgRt_ItemCommand); dgRt.PageIndexChanged+=new DataGridPageChangedEventHandler(dgRt_PageIndexChanged); dgRt.EditCommand+=new DataGridCommandEventHandler(dgRt_EditCommand); dgRt.UpdateCommand+=new DataGridCommandEventHandler(dgRt_UpdateCommand); dgRt.CancelCommand+=new DataGridCommandEventHandler(dgRt_CancelCommand); Fill the Grid:Fill the grid by pulling the data from the database. private void fillgrid() { SqlConnection cn = new SqlConnection(); SqlCommand cmd = new SqlCommand(); cmd.Connection=cn; cmd.CommandText="select * from SmpTable order by cm"; DataSet ds = new DataSet(); SqlDataAdapter adp = new SqlDataAdapter(cmd); cn.ConnectionString= "SERVER=SQLSERVERNAME;DATABASE=TestProject;UID=abcd;PWD=alskdjfh"; try { cn.Open(); adp.Fill(ds); dgRt.DataSource=ds.Tables[0]; dgRt.DataBind(); cn.Close(); } catch (Exception ex) { lblError.Text="Error Occured"; } } Write Code for databinding columns in ItemDataBound Event:private void dgRt_ItemDataBound(object sender, DataGridItemEventArgs e)
{
if((e.Item.ItemType==ListItemType.Item ) ||
(e.Item.ItemType==ListItemType.AlternatingItem))
{
DropDownList ddlSmp=((DropDownList)e.Item.Cells[7].FindControl("ddlGrid"));
ddlSmp.DataSource=.DataSource=dt;//your Data Table Name
ddlSmp.DataValueField="empid";
ddlSmp.DataTextField="EmpName";
}
}
That's it! Now, the grid is ready for you.
| ||||||||||||||||||||