Hi there

How can i get the checkbox's value from sql server 2005 and display it in my windows application?

The checkbox column in my database either has two values, checked or unchecked. Now what I want to do is I have a form where I can view data. That form displays all the data that is selected from a datagridview. The checkbox value must be displayed in checkboxes.

Any help??

You better try something different - more appropriate to use checked, or unchecked. This is to use true, false (its the same think, even better). In your database, set the column`s data type to bit. Bit can have only 2 values, this are true, or false.

Then, how to get this value out of db:
I assume you want to get one value (from one row) not all. If so, you have to use a WHERE clause that your statement can compare to sometihng.

For example, youf dgv has 2 columns (to make is simplier):
- checkBox
- costumer name

When you write the query you need to select the checkBox and complare it to the name which is in the same row, like:

stirng myQuery = @"SELECT myCheckColumn FROM Customers WHERE CustomerName = 'TheDoctered'";

If you dont want to write parameter directly in the query(in this case a string 'TheDoctered') you can pass it to the sqlCommand parameter, like:

SqlCoinnection sqlConn = new SqlConnection ("connectionStringToDB");
stirng myQuery = @"SELECT myCheckColumn FROM Customers WHERE CustomerName = 'TheDoctered'";
SqlCommand cmd = new SqlCommand(mySuery, sqlConn);
cmd.Parameters.Add("@name", SqldbType.Varchar, 50).Value = theName; //the name is parameter passed to the method of this quiery - its holds the value "TheDoctered".

If you want to populate the dgv with all the Customers from db, you dont need to use WHERE clause in the query - all others is the same!

Hope it helps,

Mitja

One of the built in column classes available for the DGV is the DataGridViewCheckBoxColumn.
This has a TrueValue property and a FalseValue property that determine the value of the data in the underlying DataTable (and database) for the checked and unchecked state of the checkbox shown in the column.
This is how to manually add a checkbox column to a DGV.

DataGridViewCheckBoxColumn column1 = new DataGridViewCheckBoxColumn();
            column1.HeaderText = "Column1";
            column1.TrueValue = "Checked"; // value in datatable for checked state
            column1.FalseValue = "Unchecked"; // value in datatable for uncheked state
            column1.DataPropertyName = "CheckBox"; // column name in datatable/database
            this.dataGridView1.Columns.Add(column1);

Note: This even allows you to have True for unchecked, and False for checked, which is sometimes useful.

Thanks for the help guys :)

If I want to add a checkbox value to the database, what type do I use??

For example:

pendingcheckbox.???????

What must be there? checkstate, checked ?

Use Checked if you only need true/false.
Use CheckState if you need the tri-state value (Indeterminate) (this shows as a filled box).

Ahhhh thank you :) The Checked property works perfect.

Problem solved guys, thanks for the help!

Docter D :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.