Question

I have a Windows Form that was mainly made using the graphical editor. It is connected to a database called, Database1. One of the tables in the database is called Table1, and contains the column CheckBox1. When I connected the form to the database, Database1DataSet.xsd and Database1DataSet.Designer.cs were automatically created.

CheckBox1 can either hold "Yes" or blank (this wasn't my decision). I would like to make a checkbox checked if the value in the CheckBox1 column is "Yes", and unchecked if the value is blank. If I drag a bound checkbox onto the form, it doesn't work because I assume that the values in the column need to be either 1 or 0. So I'm trying to work around this.

In my form, I have the following

// Form Constructor
public myForm()
{
    // Initializes all the components in the form
    InitializeComponent();

    // Change the checkboxes checked state
    this.myCheckBox.Checked = myCheckBox_Update();
}

// Method for determining if the checkbox should be checked
private bool myCheckBox_Update()
{
    // This SHOULD bring in the current record of the database
    DataRowView current = (DataRowView)this.Table1BindingSource.Current;

    try 
    {
        // This SHOULD determine if the value in the CheckBox1 field has a value
        return current.Row["CheckBox1"].ToString().Length > 0;
    }
    catch (NullReferenceException ex)
    {
        MessageBox.Show("NullReferenceException was thrown!", "Error");

        return false;
    }
}

In the InitializeComponent() function, there is the following

// This line is generated when I drag a bound checkbox to the form
// this.myCheckBox.DataBindings.Add(new System.Windows.Forms.Binding("CheckState", this.Table1BindingSource, "CheckBox1", true));

// For the miscellaneous information about the checkbox
this.myCheckBox.AutoSize = true;
this.myCheckBox.Location = new System.Drawing.Point(3, 3);
this.myCheckBox.Name = "myCheckBox";
this.myCheckBox.Size = new System.Drawing.Size(92, 23);
this.myCheckBox.TabIndex = 0;
this.myCheckBox.Text = "Check Box";
this.myCheckBox.UseVisualStyleBackColor = true;

However, it keeps throwing a NullReferenceException. I'm assuming that this is because this.Table1BindingSource.Current cannot be cast as a DataRowView.

I have looked at several other posts on SO that are somewhat related to this problem (e.g. This post, or this post), but I haven't found anything that has worked so far. The second link's answer doesn't work because I am iterating through the records with this.Table1BindingSource.MoveNext();, and I won't know the index.

Can someone steer me in the right direction? I'd really appreciate it


EDIT: The BindingSource is initialized with this

private System.Windows.Forms.BindingSource Table1BindingSource;
this.Table1BindingSource = new System.Windows.Forms.BindingSource(this.components);
Was it helpful?

Solution 2

What I ended up doing is just changing the values in my database from "Yes" and blank to 1 and 0. Then in the InitializeComponent() method, I added the following line (or it might be generated for you if you drag it from the data source panel)

this.myCheckBox.DataBindings.Add(new System.Windows.Forms.Binding("CheckState", this.Table1BindingSource, "CheckBox1", true));

OTHER TIPS

I'll put this in an answer since it's not fit for a comment.

You may also try checking the value for null in your try statement.

try 
{
    // This SHOULD determine if the value in the CheckBox1 field has a value
    object val = current.Row["CheckBox1"];
    if (val == null)
    {
        return false;
    }
    else
    { 
        return current.Row["CheckBox1"].ToString().Length > 0;
    }
}

Or at least set a breakpoint at the if (val == null) statement and make sure val has a value that you would expect.

If by 'blank' you mean a String.Empty, then your code would work as you expect it to, but if blank means null, then you'll have to check for null, you can't call ToString() on a null value.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top