I have a GridView1 declared as:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" GridLines="None" DataKeyNames="Case_ID">
<Columns>
<asp:TemplateField>
<ItemTemplate><asp:CheckBox ID="cb" runat="server" /></ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="No.">
<ItemTemplate><%# Container.DataItemIndex + 1 %></ItemTemplate>
</asp:TemplateField>
<asp:BoundField HeaderText="Case Title" DataField="caseTitle"/>
<asp:BoundField HeaderText="Age" DataField="age" />
<asp:BoundField HeaderText="Gender" DataField="gender"/>
<asp:BoundField HeaderText="Treated By" DataField="owner"/>
<asp:BoundField HeaderText="Added Date" DataField="sDate"/>
<asp:BoundField HeaderText="" DataField="Case_ID" Visible="false"/>
</Columns>
</asp:GridView>
and this is the DataTable I'm using as a DataSource for it
DataTable aTable = new DataTable();
aTable.Columns.Add("caseTitle", typeof(string));
aTable.Columns.Add("age", typeof(string));
aTable.Columns.Add("gender", typeof(string));
aTable.Columns.Add("owner", typeof(string));
aTable.Columns.Add("sDate", typeof(string));
aTable.Columns.Add("Case_ID", typeof(int));
I want to use Case_ID as an index for the GridView, so I can delete a record from database when I check its check box in the GridView.
here is the scenario:
GridView displays cases information from database User can check check-boxes and then click [Delete] button, this action should delete the records from database;
When I execute this code in [Delete] button:
foreach (GridViewRow row in GridView1.Rows)
{
CheckBox c = (CheckBox)row.FindControl("cb");
if (c.Checked)
{
int rowIndex = GridView1.SelectedIndex;
string id = GridView1.DataKeys[rowIndex].Value.ToString();
ds.DeleteCommand = " delete from Cases where Case_ID=" + id + "";
ds.Delete();
}
}
I found that c is always null even when I'm checking some records; so the problem is its not detecting the change in the checkbox what can I do to make it detected?