Create Template column dynamically in DataGrid






3.60/5 (17 votes)
Mar 8, 2006
1 min read

122335

1599
This articles helps in creating template columns dynamically with bound columns using a template class
Introduction
This Article helps in creation of template columns dynamically . I got a condition when i have to display few column values from database and few template (checkbox) column dynamically.
I have created a sample template class which is required in project.Firstly modify template class as per your requirement.As here i have given checkbox as template column example.
Steps to implement code in your project :
1) Set the DataGrid AutoGenerateColumns property to false .
2) Example to Bound the static Columns
<asp:DataGrid id="ItemsGrid" runat="server" OnItemCommand="Grid_CartCommand" AutoGenerateColumns="False"
CellPadding="3" BorderWidth="1px" BorderColor="Black">
<HeaderStyle BackColor="#C0C0FF"></HeaderStyle>
<Columns>
<asp:ButtonColumn Text="Add" ButtonType="PushButton" HeaderText="Add to cart" CommandName="AddToCart"></asp:ButtonColumn>
<asp:ButtonColumn Text="Remove" ButtonType="PushButton" HeaderText="Remove from cart" CommandName="RemoveFromCart"></asp:ButtonColumn>
<asp:BoundColumn DataField="StringValue" HeaderText="Item"></asp:BoundColumn>
<asp:BoundColumn DataField="CurrencyValue" HeaderText="Price" DataFormatString="{0:c}">
<ItemStyle HorizontalAlign="Right"></ItemStyle>
</asp:BoundColumn>
</Columns>
</asp:DataGrid>
3) In Page_load add following Code
if (!IsPostBack)
{
// Load this data only once.
ItemsGrid.DataSource= CreateDataSource(); //Function to create dynamic column
ItemsGrid.DataBind();
}
4) In OnInit() add following code protected void OnInit(EventArgs e)// // CODEGEN: This call is required by the ASP.NET Web Form Designer. //
CreateDataGridColumn();
InitializeComponent();
}
base.OnInit(e);//-----------------------------------------------------------------------------
public
{
override
{
void CreateDataGridColumn()// Create dynamic column to add to Columns collection. //-----------------------------------------------------------------------------TemplateColumn tc1 =
//-----Class used named DataGridTempla.cs------
tc1.HeaderTemplate =
tc1.ItemTemplate =
tc1.EditItemTemplate =
tc1.FooterTemplate =
ItemsGrid.Columns.Add(tc1);
new TemplateColumn();new DataGridTempla(ListItemType.Header, "Select1");new DataGridTempla(ListItemType.Item, "Select1");new DataGridTempla(ListItemType.EditItem, "");new DataGridTempla(ListItemType.Footer, "");//-----------------------------------------------------------------------------TemplateColumn tc2=
tc2.HeaderTemplate=
tc2.ItemTemplate=
ItemsGrid.Columns.Add(tc2);
new TemplateColumn();new DataGridTempla(ListItemType.Header,"Select2");new DataGridTempla(ListItemType.Item,"Select2");//-----------------------------------------------------------------------------BoundColumn NumberColumn =
NumberColumn.HeaderText="Item Number";
NumberColumn.DataField="IntegerValue";
new BoundColumn();// Add column to Columns collection.ItemsGrid.Columns.AddAt(2, NumberColumn);
}
//-----------------------------------------------------------------------------