Click here to Skip to main content
15,891,431 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I m trying while there is IsActive column is present in Gridview then Checkbox which is placed inside Gridview is checked..all code is running fine only last row checkbox is not checked..pls tell me solution

What I have tried:

C#
<pre>if (e.Row.RowType == DataControlRowType.DataRow)
        {
           
            foreach (GridViewRow row in gdvUserDetails.Rows)
            {

                for (int i = 0; i < dt.Rows.Count; i++)
                {

                    string cellText = dt.Rows[i][2].ToString();

                    CheckBox chk = (CheckBox)row.Cells[0].FindControl("chkRow");

                    if (cellText == "1")
                    {

                        chk.Checked = true;

                    }
                    else
                    {
                        chk.Checked = false;
                    }


                }

            }

        }
Posted
Updated 1-Oct-19 0:04am
Comments
phil.o 1-Oct-19 6:11am    
As a side note, you can write chk.Checked = (cellText == "1"); without losing any readability.
Richard Deeming 1-Oct-19 11:42am    
NB: Your nested loops aren't doing what you think they're doing. To be honest, I'm struggling to understand what you think they're doing; but whatever it is, they're not doing it.

You can effectively replace the code with:
if (e.Row.RowType == DataControlRowType.DataRow)
{
    string cellText = dt.Rows[dt.Rows.Count - 1][2].ToString();
    bool isChecked = cellText == "1";
    
    foreach (GridViewRow row in gdvUserDetails.Rows)
    {
        CheckBox chk = (CheckBox)row.Cells[0].FindControl("chkRow");
        chk.Checked = isChecked;
    }
}

There isn't really anything much we can do to help you - it depends so much on your data as to what happens. That's some odd code though ... I see no real point in the nested loops, given that the result of the inner loop will be the same each time ...

So, it's going to be up to you.
Fortunately, you have a tool available to you which will help you find out what is going on: the debugger. If you don't know how to use it then a quick Google for "Visual Studio debugger" should give you the info you need.

Put a breakpoint on the first line in the function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why.

Sorry, but we can't do that for you - time for you to learn a new (and very, very useful) skill: debugging!
 
Share this answer
 
Comments
CPallini 1-Oct-19 6:07am    
5.
Accordingly to your code:
C#
string cellText = dt.Rows[i][2].ToString();
CheckBox chk = (CheckBox)row.Cells[0].FindControl("chkRow");
if (cellText == "1")
{
    chk.Checked = true;
}
else
{
    chk.Checked = false;
}


Probably... in last row cellText variable does not hold "1"...
 
Share this answer
 
Comments
CPallini 1-Oct-19 6:07am    
Probably.
5.
Member 14594378 1-Oct-19 6:14am    
I Used Page size =10 and all rows which is bind is holdind 1 value ...but all Pager last row not checked..but they hold IsActive =1
Maciej Los 1-Oct-19 6:16am    
:laugh:
Thank you, Carlo.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900