0

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.

3
Contributors
3
Replies
4
Views
7 Years
Discussion Span
Last Post by fawadkhalil
0

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);
                    }

                }
0

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
            }
        }
    }

Edited by Ramesh S: n/a

0

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
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.