We help IT Professionals succeed at work.

Small problem with DataGridView checkbox control - vb.net

4,716 Views
Last Modified: 2013-11-26
Dear experts!

I have a DataGridView control on my form which is bind to a local database.
The grid contains a checkbox column.

The application allows the user to perform some queries on the datagridview control in order to "filter" data.

The problem is, that the checkboxes are shown as checked only when the datagridview shows the whole table from the database (ie, no select statements). If the user performs a query on the table, then even if the checkboxes are checked - they won't look like they are indeed checked.

Why is this happening and how can this be solved?

Thanks in advance!
Comment
Watch Question

"then even if the checkboxes are checked - they won't look like they are indeed checked."

I cannot understand what you mean by the above statement.Can you explain this?

Author

Commented:
:-) Sorry:

The datagrid contains data from a local .mdb file.

Everything is OK when the whole data is shown inside the table.

BUT,
In I case I want to perform a 'Select' on the table, then all check boxes apears as "unchecked".
So basically - I am able to see the checkboxes's real status only when the whole data is shown - without any 'Select' statements.

Thanks!
Can you tell let me know what queries u used for both ?

Author

Commented:
Hi,
the grid contains 6 columns, the user may perform a Select based on two column names. The 'Select' statement does not include the 6st column which is the checkbox column.
I am still not very clear but if you are not including the 6th column which is the checkbox column, how will the grid display the checks without the data?

Author

Commented:
Here's a quick example:

-------------------------------------------------------------------------------------------
USING THE DEFAULT SELECT STATEMENT (ALL COLUMNS ARE INCLUDED)
-------------------------------------------------------------------------------------------

Column (1)        Column(2)       Column(3)    Column(4)    Column (5 - Check box column)
======================================================================
some vaule        another vaule                                           Checked <------------
another value          .......                                                    Checked
and another value   .......                                                     Not Checked

---------------------------------------------------------------------------------------
SELECT ("COLUMN (1) = 'some value' AND Column (2) = 'another value'
---------------------------------------------------------------------------------------
Result:
--------

Column (1)        Column(2)       Column(3)    Column(4)    Column (5 - Check box column)
======================================================================
some vaule        another vaule                                           NOT CHECKED <-------
                                                                                            -----------------

**************************************************
Notice the check box status, when ALL data is shown, the check box is correct. When using the Select statement (see figure 2) - check box appears as unchecked.

Thanks!
1) Is column 5 a Char value or a kind of Bool value?
2) Are all these columns unbound columns?
3) FOr case 1) manually key in all the columns instead of keying in *

Author

Commented:
Hi surajguptha,

Yes, column 5 is a Char value ("Yes" - checked, "No" - unchecked).
Can you be more specific about section number (3)?

Will converting column 5 into a bool value make things easier? If yes, how can this be done?

Thanks again!
3)

If you did a select * from employee it would internally convert it into select col1,col2,col3,col4 from the database. where col1,col2,col3.col4 are all the columns of the table

Can you try doing that? Can you convert your * and replace it with all the actual columns?

Author

Commented:
I know how to do this with a "regular" sql statement,
but since I'm using the .Select() method of the datagridview control, I don't know how to achieve the same results as I would with my original select query.

Can you post an example of such query?

What exact query string are u using within the select statement??

Author

Commented:
As I posted above:

SELECT ("COLUMN (1) = 'some value' AND Column (2) = 'another value'
You seem to be doing everything right. I am not sure why this happens for one and not for the other. ITs silly but have u checked if all the values that get filtered actually dont have column 5 as Y or True ??

Author

Commented:
Well,
I don't need to check them - or don't want to :-)

I just want them to show their real status after the select command, regardless of the filtered data.
Its difficult to be understand over here without the code and stuff so can you try one more thing
Can you put in some silly select condition like 1=1 or a condition that would select all the items ?? But a select filter should be present. Lets see if it still removes the checks?

Author

Commented:
Yes, I'll try that soon - I'll post back and let you know later on, I must leave now.

And thanks again!
Your help is really appreciated!

Commented:
nahumba

In post #19983844 you said "... since I'm using the .Select() method of the datagridview control ...".  The datagridview doesn't have a .Select method of this sort.  Its .Select method is inherited from Control, and is only concerned with selecting a sub control, not a record or records, within the main control.

Also, whatever method it is you are actually using, the expression

SELECT ("COLUMN (1) = 'some value' AND Column (2) = 'another value'

that you included in posts #19983724 and #19984221 would fail.  The opening bracket and double quotes are not closed.

So can you please post the code that you are actually using?

Roger

Author

Commented:
Hi Roger,
Here's the correct query:
.SELECT ("COLUMN (1) = 'some value' AND Column (2) = 'another value'")

That's the method I'm using - the only difference are the values and column names - but that's the syntax.

Thank you!!!!

Commented:
But that cannot be for a DataGridView.  What is the object that goes before the point?

Roger

Author

Commented:
Oh sorry,
Here's how I do it:

Dim MainSelect As DataRow()
MainSelect = Me.DataSet1.Table1.SELECT ("COLUMN (1) = 'some value' AND Column (2) = 'another value'")
DataGridView2.DataSource = MainSelect
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:
Roger - it now works like a charm!


Thank you so much!!!

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.