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.

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.