Reputation: 1497
I have a gridview that displays data coming from an SQL data source (stored procedure) and I want to add a checkboxex column to it, here is my code:
TemplateField field = new TemplateField();
field.HeaderText = "Exporter ?";
gv.Columns.Add(field);
CheckBox cb = new CheckBox();
cb.Visible = true;
The problem is that I don't know how to add a checkbox to the TemplateField that I added to my gridview columns.
Upvotes: 1
Views: 1769
Reputation: 4294
1) Add below code to GridView columns.
<asp:TemplateField HeaderText="CheckBoxColumn" Visible="False">
<ItemTemplate>
<asp:CheckBox ID="checkBox" runat="server"></asp:CheckBox>
</ItemTemplate>
</asp:TemplateField>
2) Make checkbox column visible dynamically by adding OnRowDataBound event or just looping through GridView.Rows
int indexOfCBColumn = 0;
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
e.Row.Cells[indexOfCBColumn].Visible = true;
}
foreach(GridViewRow row in GridView1.Rows) {
if(row.RowType == DataControlRowType.DataRow) {
row.Cells[indexOfCBColumn].Visible = true;
}
}
Thanks!!
Upvotes: 1
Reputation: 511
You can add the code below into the GridView
Columns.
<asp:templatefield HeaderText="Check Box">
<itemtemplate>
<asp:checkbox ID="cb" runat="server"></asp:checkbox>
</itemtemplate>
</asp:templatefield>
Upvotes: 0