in my procedure in code-behind I am trying to create a DataGrid with autoGenerateColumns = true
:
DataGrid NewDg = new DataGrid();
NewDg.AutoGenerateColumns = true;
NewDg.Width = Unit.Percentage(100.00);
NewDg.DataSource = ds;
NewDg.DataBind();
I need to also add another column (TemplateColumn
maybe) of CheckBoxes to this Grid.
Do you know how to do that??
I have tried that:
TemplateColumn t = new TemplateColumn();
CheckBox c = new CheckBox();
t.ItemTemplate = (ITemplate)c;
NewDg.Columns.Add(t);
I get the following exception trace:
System.InvalidCastException: Unable to cast object of type 'System.Web.UI.WebControls.CheckBox' to type 'System.Web.UI.ITemplate'. at Default2.DataGrid1_ItemDataBound(Object sender, DataGridItemEventArgs e) in c:\Users\saher\Documents\TreeTest\TreeDemo\Default2.aspx.cs:line 116 at System.Web.UI.WebControls.DataGrid.OnItemDataBound(DataGridItemEventArgs e) at System.Web.UI.WebControls.DataGrid.CreateItem(Int32 itemIndex, Int32 dataSourceIndex, ListItemType itemType, Boolean dataBind, Object dataItem, DataGridColumn[] columns, TableRowCollection rows, PagedDataSource pagedDataSource) at System.Web.UI.WebControls.DataGrid.CreateControlHierarchy(Boolean useDataSource) at System.Web.UI.WebControls.BaseDataList.OnDataBinding(EventArgs e) at System.Web.UI.WebControls.BaseDataList.DataBind() at Default2.BindData() in c:\Users\saher\Documents\TreeTest\TreeDemo\Default2.aspx.cs:line 44