views:

127

answers:

3

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 
+1  A: 

You'll need to add the checkbox to your template container instead of casting to it.

Here's an article on adding templates dynamically:
http://msdn.microsoft.com/en-us/library/aa712868(VS.71).aspx

Even Mien
Thanks! the site is really helpful! it worked!
Saher
+2  A: 

If this additional column is always present, is there a reason why you can't add it to the grid in design view as opposed to code? Or is it only displayed under certain conditions?

If not, you can specify your additional column in the design view and have the autoGenerateColumns add the rest of the columns at runtime.

Tanner
its generated under certain conditions actually
Saher
A: 

Hi,

Do you have to create it in code-behind? If so check in http://msdn.microsoft.com/en-us/library/system.web.ui.templatebuilder.aspx. Otherwise you can declare it as:

<asp:DataGrid id="MyGrid" runat="server">
    <Columns>
        <asp:TemplateColumn>
            <HeaderTemplate><b>Check Me!</b></HeaderTemplate>
            <ItemTemplate>
                <asp:CheckBox runat="server" />
            </ItemTemplate>
        </asp:TemplateColumn>
    </Columns>    
</asp:DataGrid>
Jocke
I am also implementing that in one of my static datagrids. How can I check with checkbox is checked and which is not?? if only one id will be given?
Saher
Well, it depends on how/when you want to check the value? If you loop through the grid you get the value as:foreach (DataGridItem item in MyGrid.Items){ if ((item.FindControl("id_of_CheckBox") as CheckBox).Checked) { /*logic here!*/ }}
Jocke