0

I am use Visual Studio 2008 to coding ASP.NET in C#

I am trying to get value checked or unchecked form checkbox in Gridview. This is my code to do.

void imbTransferBySelect_Click(object sender, ImageClickEventArgs e)
{
    for (int i = 0; i < GridView1.Rows.Count; i++)
    {
        CheckBox cb = (CheckBox)GridView1.Rows[i].FindControl("Transfer_Selector");
        if (cb.Checked)
        {
            //do something
        }
    }
}

When the checkbox is unchecked the code runs fine.

But when checkbox is checked value of GridView1.Rows.Count = 0 it's make for loop is fail

I try use another code in http://www.aspsnippets.com/Articles/GridView-with-CheckBox-Get-Selected-Rows-in-ASPNet.aspx

foreach (GridViewRow row in GridView1.Rows)
{
    if (row.RowType == DataControlRowType.DataRow)
    {
        CheckBox chkRow = (row.Cells[0].FindControl("Transfer_Selector") as CheckBox);
        if (chkRow.Checked)
        {
            //do something
        }
    }
}

It's runs fine when check-box is not checked.

And when check-box checked it's make loop in for-each fail again.

Anyone ever had a problem like me or know what's happened.

4
  • What means "when checkbox is checked value of GridView1.Rows.Count = 0 it's make for loop is fail"? How can a checkbox be checked if there is no row with a CheckBox? Commented Feb 12, 2015 at 8:13
  • Yes, when check-box checked the GridView1.Rows.Count = 0, I see it in debug step, and when code run finished the gridview is missing Commented Feb 12, 2015 at 8:20
  • I try to change check-box default is checked in .aspx file and run again, it's fail again but when i unchecked all, it's can run in for loop and it's enter the checked condition. Sorry for my problem, It should make you confuse. Commented Feb 12, 2015 at 8:37
  • Can you put your gridview html in your code. Aslo the code where you binding gridview. Commented Feb 12, 2015 at 8:42

1 Answer 1

1

Buddy just put one if condition on foreach loop according to your question i am giving you solution. check below solution..

if(GridView1.Rows.Count > 0 )
{
foreach (GridViewRow row in GridView1.Rows)
{
    if (row.RowType == DataControlRowType.DataRow)
    {
        CheckBox chkRow = (row.Cells[0].FindControl("Transfer_Selector") as CheckBox);
        if (chkRow.Checked)
        {
            //do something
        }
    }
}
}
Sign up to request clarification or add additional context in comments.

5 Comments

GridView1.Rows.Count = 0 and it's not enter to the condition
you just add this condition -> if(GridView1.Rows.Count > 0 )
I coding like this and when i running GridView1.Rows.Count = 0 so it's not enter to if and for-each condition.
What you want to do? exactly??!!
I want to get name from checked row and change they managerID from database

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.