0

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?

1 Answer 1

0

Where's the delete button... try finding the fitting checkbox button by using the "argument" attribute of the delete button. I usually put the ID of the row that I wish to delete as the argument attribute value of the button. That's why I never need to search for the checkbox... (on client side I use the client click event of the checkbox to update the argument attribute of the delete button).

Another word on security: use stored procedure to avoid security issues (never put delete statement - or any SQL statement - as hard coded in C#...).

EDIT:

check also:

     GridViewRow.RowType==RowType.DataRow 

You get null because the current row is possibly an header row, or something like that - and the checkbox is not a part of this kind of row.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for replaying, the delete button appears after the gridview: <asp:Button ID="Button1" runat="server" Text="Delete" onclick="Button1_Click" /> I'm sorry I don't know how I can use the ID as an argument of the button, it could be more than one checked check-box, is it possible to assign more than one ID?
@Meensat I edited my answer with another check you need to do for each row. But, I've got to say - you must improve it - try work delete link button on each row, then you would not have to search for the rows that has to be deleted...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.