c# - Add TemplateColumn of CheckBoxes to DataGrid programmatically -


In code-back in my process I'm trying to create a data grid with autoGenerateColumns = true :

  datagrid NewDg = new data grid (); NewDg.AutoGenerateColumns = True; NewDg.Width = Unit Percent (100.00); NewDg.DataSource = DS; NewDg.DataBind ();  

I also need to add another column (maybe TemplateColumn ) of the check box for this grid. How do you know how to do just that?

I have tried that:

  TemplateColumn T = new TemplateColumn (); Check box C = new checkbox (); T.ItemTemplate = (ITemplate) c; NewDg.Columns.Add (t);  

I find the following exception:

  System.InvalidCastException: unable to cast object of type 'System.Web.UI.WebControls.CheckBox' Type 'System.Web.UI.itemplate' in Type2.DataGrid1_ItemDataBound (Object Sender, DataGridItemEventArgs E) in: \ Users \ saher \ Document \ TreeTest \ TreeDemo \ Default2.aspx.cs: Line System.Web.UI. WebControls.DataGrid.OnItemDataBound (DataGridItemEventArgs E) at 116 System.Web.UI.WebControls.DataGrid.CreateItem (Int32 itemIndex, Int32 dataSourceIndex, ListItemType itemType, Boolean Databand, Object DataTime, DataGrid column [] Columns, TableRocking Rows, Pages Datasource pageed data) system. Web. UI. WebControl .DataGrid Creole Control at highway C. (Boolean useDataSource) on System.Web.UI.WebControls.BaseDataList.OnDataBinding (EventArgs e) System.Web.UI.WebControls.BaseDataList.DataBind () on Default2.BindData (): saher \ \ Documents \ Users \ TreeTest \ TreeDemo \ Default2.aspx.cs: line 44  

Instead of adding a checkbox to casting your template container to this,

Here is an article on adding dynamic templates:

Milliliters>

Comments