adding check box dynamically to gridview
<asp:GridView ID="SearchGrid" runat="server" GridLines="Both" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="Q" HeaderText="Q" />
<asp:TemplateField HeaderText="C">
<ItemTemplate>
<asp:CheckBox ID="CCheckbox" runat="server" AutoPostBack="true" OnCheckedChanged="CCheckbox_CheckedChanged" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="R">
<ItemTemplate>
<asp:CheckBox ID="RCheckbox" runat="server" AutoPostBack="true" OnCheckedChanged="RCheckbox_CheckedChanged" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="E">
<ItemTemplate>
<asp:CheckBox ID="ECheckbox" runat="server" AutoPostBack="true" OnCheckedChanged="ECheckbox_CheckedChanged" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
This is my Aspx code for the GridView
if (ViewState["SearchGrid"] == null)
{
SearchTable.Columns.Add(new DataColumn("Q", typeof(string)));
SearchTable.Columns.Add(new DataColumn("C", typeof(bool)));
SearchTable.Columns.Add(new DataColumn("R", typeof(bool)));
SearchTable.Columns.Add(new DataColumn("E", typeof(bool)));
ViewState["SearchGrid"] = SearchTable;
}
SearchTable =(DataTable) ViewState["SearchGrid"];
DataRow dr = null;
dr = SearchTable.NewRow();
dr["Q"] = MySearchTextBox.Text;
switch (SearchType)
{
case "Contains": dr["C"] =true;
break;
case "Related": dr["R"] = true;
break;
case "Exact": dr["E"] = true;
break;
}
and my code behind.
At the time of adding the row I have to check a paticular check box. I have been trying to check the checkbox by giving its values as true but it does not seem to work. Can someone tell me what I am doing wrong here?
You can bind in the markup to the value: Checked='<%# Eval("C") %>'
<asp:GridView ID="SearchGrid" runat="server" GridLines="Both" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="Q" HeaderText="Q" />
<asp:TemplateField HeaderText="C">
<ItemTemplate>
<asp:CheckBox ID="CCheckbox" runat="server" AutoPostBack="true" OnCheckedChanged="CCheckbox_CheckedChanged" Checked='<%# Eval("C") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="R">
<ItemTemplate>
<asp:CheckBox ID="RCheckbox" runat="server" AutoPostBack="true" OnCheckedChanged="RCheckbox_CheckedChanged" Checked='<%# Eval("E") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="E">
<ItemTemplate>
<asp:CheckBox ID="ECheckbox" runat="server" AutoPostBack="true" OnCheckedChanged="ECheckbox_CheckedChanged" Checked='<%# Eval("R") %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Or you can use rowdatabound event: OnRowDataBound="SearchGrid_RowDataBound"
MarkUP Change:
<asp:GridView ID="SearchGrid" runat="server" GridLines="Both" AutoGenerateColumns="false" OnRowDataBound="SearchGrid_RowDataBound">
Codebehind: Note i used a list object instead of a datatable.
public class TestObject
{
public string Q { get; set; }
public bool C { get; set; }
public bool R { get; set; }
public bool E { get; set; }
}
public partial class Default : System.Web.UI.Page
{
public List<TestObject> Values { get; set; }
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
//NEED SOME DATA TO TEST THE RESULT
Values = new List<TestObject>();
Values.Add(new TestObject() { Q = "test 1", C = true, E = true, R = true });
Values.Add(new TestObject() { Q = "test 1", C = true, E = false, R = true });
Values.Add(new TestObject() { Q = "test 1", C = true, E = true, R = false });
Values.Add(new TestObject() { Q = "test 1", C = false, E = true, R = true });
//BIND TO THE GRID
SearchGrid.DataSource = Values;
SearchGrid.DataBind();
}
}
//FIRES FOR EVERY ROW IN THE GRID
protected void SearchGrid_RowDataBound(object sender, GridViewRowEventArgs e)
{
//WE ONLY CARE ABOUT THE DATAROW NOT HEADER ETC
if (e.Row.RowType != DataControlRowType.DataRow)
return;
//GET THE BOUND INDIVIDUAL ITEM
TestObject obj = (TestObject)e.Row.DataItem; //IN YOUR CASE THIS WOULD BE: DataRow row = (DataRow)e.Row.DataItem
//FIND ALL THE CHECKBOXES
CheckBox cchk = e.Row.FindControl("CCheckBox") as CheckBox;
CheckBox echk = e.Row.FindControl("ECheckBox") as CheckBox;
CheckBox rchk = e.Row.FindControl("RCheckBox") as CheckBox;
//CHECK IT OR NOT BASED ON THE DATATITEMS VALUE
if (cchk != null)
cchk.Checked = obj.C; //IN YOUR CASE THIS WOULD BE: bool c = (bool)row["C"];
if (echk != null)
echk.Checked = obj.E;
if (rchk != null)
rchk.Checked = obj.R;
}
}