Tom Knowlton
asked on
Add checkbox to programmatically generated GridView
<Columns>
<asp:TemplateField>
<itemtemplate>
<asp:CheckBox ID="CheckBoxRowItem" runat="server" />
</itemtemplate>
</asp:TemplateField>
</Columns>
How do I do this programmatically in the C# code behind?
This checkbox will NOT be databound.
My current method:
private void BuildGridView(DataView dv)
{
this.GridViewSearchResults .Columns.C lear();
this.GridViewSearchResults .HeaderSty le.Horizon talAlign = HorizontalAlign.Left;
/////////////////DOES NOT SEEM TO WORK:
//CheckBoxField cbf = new CheckBoxField();
//this.GridViewSearchResul ts.Columns .Add(cbf);
foreach (DataColumn dc in dv.Table.Columns)
{
BoundField bf = new BoundField();
bf.HeaderText = dc.ColumnName;
bf.DataField = dc.ColumnName;
bf.HeaderStyle.ForeColor = System.Drawing.Color.White ;
bf.HeaderStyle.BackColor = System.Drawing.Color.Black ;
bf.SortExpression = dc.ColumnName;
this.GridViewSearchResults .Columns.A dd(bf);
}
this.GridViewSearchResults .AutoGener ateColumns = false;
this.GridViewSearchResults .DataSourc e = dv;
this.GridViewSearchResults .DataBind( );
}
<asp:TemplateField>
<itemtemplate>
<asp:CheckBox ID="CheckBoxRowItem" runat="server" />
</itemtemplate>
</asp:TemplateField>
</Columns>
How do I do this programmatically in the C# code behind?
This checkbox will NOT be databound.
My current method:
private void BuildGridView(DataView dv)
{
this.GridViewSearchResults
this.GridViewSearchResults
/////////////////DOES NOT SEEM TO WORK:
//CheckBoxField cbf = new CheckBoxField();
//this.GridViewSearchResul
foreach (DataColumn dc in dv.Table.Columns)
{
BoundField bf = new BoundField();
bf.HeaderText = dc.ColumnName;
bf.DataField = dc.ColumnName;
bf.HeaderStyle.ForeColor = System.Drawing.Color.White
bf.HeaderStyle.BackColor = System.Drawing.Color.Black
bf.SortExpression = dc.ColumnName;
this.GridViewSearchResults
}
this.GridViewSearchResults
this.GridViewSearchResults
this.GridViewSearchResults
}
Instead of adding a BoundField, you could add a CheckBoxField in your foreach.
ASKER
Please provide code.
Remember I want the checkbox to be in the far left column, and it will be unbound.
Remember I want the checkbox to be in the far left column, and it will be unbound.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Will a CheckBoxField still work if it is unbound?
Yes you don't need to bind it.
ASKER
It is not showing up.
the other fields show up, but not the checkbox.
the other fields show up, but not the checkbox.