Solved

How do I set the value of a bound checkbox to False?

Posted on 2007-12-04
5
204 Views
Last Modified: 2013-12-25
I have an order form in my ADO.Net app. On the form is a checkbox that I would like the CheckState to be set so its default value is "Fasle".  When the "ADd New" button is clicked, the checkbox has a highlighted green square in it. I would like this to be empty (False). I would like that value to automatically drop into the database when it is updated. I've tried:
Checkbox.CheckState = CheckState.Unchecked
I've also tried to set the default value in the databse column for "False" but this won't work either.
How do I set the value to be "False" ?

Thanks
0
Comment
Question by:grouchyfd
  • 3
  • 2
5 Comments
 
LVL 27

Expert Comment

by:VBRocks
Comment Utility
Is the DataGridView bound to a DataSource?  

For example, do you set the DataSource to a DataTable or DataView, like this:
    Me.DataGridView1.DataSource = table

If you do, then you can just add a new row to the underlying table, and set the value before you add it.
Demonstration:
        'Example creating a DataTable to use as the source:
        Dim table As New DataTable()
        table.Columns.Add("IsSelected")
        table.Columns("IsSelected").DataType = GetType(Boolean)
        table.Columns.Add("Item")

        table.Rows.Add(False, "Item 1")

        Me.DataGridView1.DataSource = table


        'Example adding the new row:
        Dim table As DataTable
        table = Me.DataGridView1.DataSource

        Dim row As DataRow = table.NewRow()

        'Set the column to the Checked property in your Checkbox
        row("IsSelected") = Me.CheckBox1.Checked
        row("Item") = "Item 2"

        table.Rows.Add(row)


0
 
LVL 27

Accepted Solution

by:
VBRocks earned 500 total points
Comment Utility
If your DataGridView is not bound to a DataSource as demonstrated above, then just use the
following approach:

    Me.DataGridView1.Rows.Add(Me.CheckBox1.Checked, "Item 1")

0
 

Author Comment

by:grouchyfd
Comment Utility
I have the controls on the form  in the "Details" mode, not the DataGridView so the CheckBox is box with a label on the form. I've use the code frmOrderForm.CheckBox.CheckState.Unchecked. When I put this in the intellisense tells me to enter it as CheckState. Unchecked but then this comes up a s error also.
0
 

Author Comment

by:grouchyfd
Comment Utility
I've gone into the table designer and changed the default property of the checkbox to "False". Then I get a different error when I try to update the database.
0
 

Author Comment

by:grouchyfd
Comment Utility
I finally solved the problem.
I went to my data source using the DataSource window, clicked on the dataset, then opened it by using the  "Edit DataSet with Designer". Clicked on the row of the cloumn I needed to change the default value of, which opened the Properties Window for this row and changed the "Default Value" to "False". Now every time a row is created for a new order, the default of "False" ( int 0) is automatically entered into the database cloumn.
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now