tags:

views:

537

answers:

3

I have a dataGrid(not dataGridView) in which i need to add Checkbox dynamically at the first column. How can i do this.?

A: 

The following woprks with a GridView, and I believe that it's also the same for a DataGrid.

Just add a Template Column. (You can do that in source or via the GUI). Then add a checkbod to the ItemTemplate:

    <Columns>
        <asp:TemplateField>
            <HeaderTemplate>
                Retry
            </HeaderTemplate>
            <ItemTemplate>
                <asp:CheckBox ID="CheckBox1" runat="server" />
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>

ps. Might want to consider dropping in a GridView if you're on 2.0+

Mark Maslar
the request is for dynamically, this is just basically how you add a checkbox
TStamper
A: 

I don't believe you can add them to the first column dynamically if the datagrid already has columns, because it would just append the new column to a datagrid by using the Add method, making it the last column:

CheckBoxColumn checkCol = new CheckBoxColumn();
DataGrid1.Columns.Add(checkCol);

but to add them dynamically you could either follow thse steps at CodeProject Adding a CheckBox column to your DataGrid

or

If you still want the look of them being in the first column, then you can just create them in your client side code and set their visibile attribute to false and when you conditions are met in code behind then set the attribute to true, which gives the idea that it was crated dynamically

TStamper
A: 

In the ItemDataBound event, which fires for each row of the DataGrid, you could dynamically add a control to the first cell. Easier to use a TemplateColumn, but if you want to do it dynamically as you asked, this is how I'd do it.

private void DataGrid1_ItemDataBound(object sender, 
                 System.Web.UI.WebControls.DataGridItemEventArgs e) 
{
    if ((e.Item.ItemType == ListItemType.AlternatingItem) || 
                 (e.Item.ItemType == ListItemType.Item)) 
    {
        CheckBox chk = new Checkbox();
        e.Item.Cells[0].Controls.Add(chk);
    }
MikeW