Adding checkbox dynamically to dataGrid in C # Visual studio 2005
In the ItemDataBound event that fires for each row of the DataGrid, you can dynamically add the control to the first cell. It's easier to use TemplateColumn, but if you want to do it dynamically as you asked, here's how I would 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);
}
a source to share
Following woprks with GridView and I believe it is the same for DataGrid too.
Just add a column of templates. (You can do this in source or through a GUI). Then add checkbod to ItemTemplate:
<Columns>
<asp:TemplateField>
<HeaderTemplate>
Retry
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
ps. You might want to consider resetting to the GridView if you are using 2.0+
a source to share
I don't believe you can add them to the first column dynamically if the datagrid already has columns, because it will just add a new column to the datagrid using the Add method, making it the last column:
CheckBoxColumn checkCol = new CheckBoxColumn();
DataGrid1.Columns.Add(checkCol);
but to add them dynamically you can either follow these steps in the CodeProject Adding a CheckBox Column to the DataGrid
or
If you still want their appearance in the first column, you can simply create them in your client code and set their visible attribute to false, and when the conditions are met in the code behind, set the attribute to true, which gives an idea of that it was broken dynamically
a source to share