Solved

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

Posted on 2007-12-04
5
207 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
ID: 20403320
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
ID: 20403346
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
ID: 20403645
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
ID: 20403689
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
ID: 20404940
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
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…

828 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