Click here to Skip to main content
15,868,016 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a DataGridView with a CheckBox first column.

I use the following Linq to get all the checked rows.

C#
DataGridViewRow[] drs = dgvMain.Rows.Cast<DataGridViewRow>().Where(x =>(!Convert.IsDBNull(x.Cells[0].Value) && Convert.ToBoolean(x.Cells[0].Value))).ToArray();


But somehow the result ALWAYS missing the last row!!!

BUT, if I select another roll (not checking it), before I run the line, the last row showed up!!!

Could somebody please be so kind and tell me where did I do wrong!?

Much appreciated!!!

What I have tried:

I had checked all kind of where condition to no avail!
Posted
Updated 6-Mar-19 13:28pm
v2
Comments
RickZeeland 1-Feb-19 2:22am    
Is the last row empty or does it have a null value ?
George Swan 2-Feb-19 2:52am    
It seems to me that the last row must be in 'edit mode' when you run the query. That's why, when you select another row, the problem resolves.

We can't tell: this is very likely dependant on your data, and we don't have any access to that.

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.

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. Examine the DataSource content, and find out exactly what it will be processing. 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. Look at the array and see exactly what it contains.

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
 
George Swan is right! The DataGridView IS still in edit mode!

So adding
C#
dgvMain.EndEdit();
before the Linq solved the problem!
 
Share this answer
 
Review This Code:

C#
DataTable dt = new DataTable();
            dt.Columns.Add("A", typeof(Boolean));
            dt.Columns.Add("B", typeof(string));
            dt.Rows.Add(true,"A");
            dt.Rows.Add(true, "B");
            dt.Rows.Add(false, "C");
            dt.Rows.Add(true, "D");
            dt.Rows.Add(false, "E");
            dgvMain.DataSource = dt;

 DataGridViewRow[] drs = dgvMain.Rows.Cast<DataGridViewRow>().Where(x =>
           (!Convert.IsDBNull(x.Cells[0].Value) && Convert.ToBoolean(x.Cells[0].Value)))
           .ToArray();
 
Share this answer
 
Comments
OriginalGriff 1-Feb-19 3:24am    
Reason for my vote of one: Not an answer.
This doesn't show anything - if the last row had been 'true, "E"' it might have demonstrated that the code works, but with 'false, "E"' it shows exactly what the user is talking about and adds nothing to the discussion.

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