1

I'm using Select on a datatable and am getting unexplainable results. Using Visual Studio 2013 and VB.NET.

I have a dataset which is the datasource of a datagridview. The dataset has a column which contains boolean values, which are represented as a checkbox by the datagridview. I'm using the solution from jsturtevand in this thread to make sure the dgv is updated after the user moves the mouse away from the checkbox. Each time the user changes the checkbox value, a corresponding row is updated in the database.

In my form the user can click on apply and a bunch of actions will be executed depending on which checkboxes are checked. The code I use to find the selected items is:

 Dim rows() As DataRow
 rows = dsFiles.Tables(0).Select("isActive = True")

When the user selects the checkboxes, does another action that changes focus and then presses apply, I get the correct results. If the user changes a checkbox and presses apply immediately the select statement won't find the last change. So if he has two items selected, selects a third and immediately presses apply, the select statement only finds two items. Here is the weird part: If I check the dataset before executing select, it contains the correct data and if I use a loop like this

    For Each row As DataRow In dsFiles.Tables(0).Rows
        If row("isActive") = True Then count += 1
    Next

then I get the correct number of items. Which means .Select returns different data then when I do the same thing it is supposed to do by hand. And how is any of this related to the user pressing apply before leaving focus when the dgv, the database and the dataset are all updated correctly? Everything works fine, and yet .Select just doesn't work. Of course I could just as well use a loop, but I want to know how this is even possible.

Edit: Wrongly assumed .Select was a Linq method. Removed references to Linq.

Community
  • 1
  • 1
user2696330
  • 143
  • 11
  • 2
    `Select("isActive = True")` is not Linq `Select` extension method, it is DataTable method. https://msdn.microsoft.com/en-us/library/det4aw50(v=vs.110).aspx – ASh Jun 28 '16 at 11:59
  • 2
    Before filtering, add 'dataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit)' to commit the checkbox changes and then do the count. btw, you are not using LINQ there. – Sarvesh Mishra Jun 28 '16 at 12:12
  • You might want to look at what those other "bunch of actions" are doing: they might be undoing something. Normally, the change would flow thru to the underlying datatable when they leave the row. But your Select code will be acting on the DT rows not the DB rows (depending on how/when you update). – Ňɏssa Pøngjǣrdenlarp Jun 28 '16 at 13:34
  • @Sarvesh Mishra I tried calling'dataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit). It still only selects two rows instead of 3. – user2696330 Jun 28 '16 at 13:52
  • @Plutonix: The part that confuses me is that I checked the dgv, db and the datatable itself before using .Select. They all contain the right data. When I use the for each loop I get the right result. So the Datatable definitely contains the correct values – user2696330 Jun 28 '16 at 13:54
  • Why not add a button column? A checkbox just doesnt scream "do something right this instant" to the user – Ňɏssa Pøngjǣrdenlarp Jun 28 '16 at 14:33
  • @Plutonix Well, I don't really want to go too much into the details of what I'm doing, but it makes sense to use a checkbox in that context. Either way, I have a solution: Use For Each instead of .Select. It works fine, but I can't for the life of me figure out why .Select returns the wrong result when for each doesn't. Does For Each and Select not work on the same data? Shouldn't both just go through the rows of the datatable? – user2696330 Jun 29 '16 at 07:00

0 Answers0