Hello guys
I have datagridview with each record containing a checkbox.

I need to loop through the grid rows and get checkboxes that is being checked and also row numbers.

There is also a delete button outside gridview.The above process should got executed on delete button click.

Please provide some source code.

Dani AI

Generated

A few likely causes for the symptom you describe (a checked row not being removed but the row below it is) are: rebinding the GridView before you read the posted checkbox states, deleting while walking the Rows collection (which can change indexes), or not using the grid DataKey correctly. Both and are on the right track locating the CheckBox in each row, but the safe pattern is to (1) read and collect the row keys first, then (2) perform deletes, then (3) rebind once.

Checklist before deleting

  • Make sure GridView.DataKeyNames is set to your primary key (InvoiceID).
  • Do not call your data-bind method on Page_Load on every request; wrap it with If Not IsPostBack (or the C# equivalent). Rebinding before reading checkboxes will lose the user selections.
  • Avoid deleting rows directly while iterating the GridView.Rows collection; collect the keys to delete first.

Example pattern (keep this behavior, adapt names to your page)

// C#
var toDelete = new List<int>();
for (int r = 0; r < grid.Rows.Count; r++)
{
    var cb = grid.Rows[r].FindControl("chkCurrentAD") as CheckBox;
    if (cb != null && cb.Checked)
        toDelete.Add(Convert.ToInt32(grid.DataKeys[r].Value));
}
foreach (var id in toDelete)
    clsinvoice.DeleteInvoice(id);
BindGrid();
' VB.NET
Dim toDelete As New List(Of Integer)
For i As Integer = 0 To grdInvoices.Rows.Count - 1
    Dim cb = TryCast(grdInvoices.Rows(i).FindControl("chkCurrentAD"), CheckBox)
    If cb IsNot Nothing AndAlso cb.Checked Then
        toDelete.Add(Convert.ToInt32(grdInvoices.DataKeys(i).Value))
    End If
Next
For Each id As Integer In toDelete
    clsinvoice.DeleteInvoice(id)
Next
BindGrid()

If the wrong ID still gets passed, add a quick log/debug print of the DataKey you read before calling DeleteInvoice to confirm the mapping. If paging is enabled, note that Rows only contains the current page — persist selections across pages if you need multi-page deletes.

Recommended Answers

All 3 Replies

on button click write this piece of code

CheckBox chk;
ArrayList CheckedItems = new ArrayList();
foreach (GridViewRow row in GrdViewTableName.Rows) {
//GridViewTableName is my gridview name
                    chk = (CheckBox)row.FindControl("chkComplete");
//chkComplete is the checkbox id
                    i++;
                    if (chk.Checked) {
                        CheckedItems.Add(i);
                    }

                }

Hello guys
I have datagridview with each record containing a checkbox.

I need to loop through the grid rows and get checkboxes that is being checked and also row numbers.

There is also a delete button outside gridview.The above process should got executed on delete button click.

Please provide some source code.

Try this code.

protected void btnDelete_Click(object sender, EventArgs e)
    {
 
        foreach (GridViewRow grow in GridView1.Rows)
        {
            CheckBox chkStat = grow.FindControl("chkStatus") as CheckBox;
            int index = grow.RowIndex;

            if (chkStat.Checked)
            {
                //Write some code if checkbox is checked
            }
            else
            {
                //Write some code if checkbox is not checked
            }
        }
    }

Hi Ramesh
i have used your code but one other problem arises is row that is checked is not deleted but the row just below it is deleted.
Here are my lines of code

Protected Sub btnDelete_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnbtnDelete.Click
Dim invoiceID As Integer
  For Each row As GridViewRow In grdInvoices.Rows

                Dim chkStat As CheckBox = TryCast(row.FindControl("chkCurrentAD"), CheckBox)
                Dim index As Integer = row.RowIndex
                If chkStat.Checked Then
                    invoiceID = grdInvoices.DataKeys(index).Value
                    clsinvoice.DeleteInvoice(invoiceID, chkStat.Checked)

                End If

            Next
End Sub
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.