admin管理员组

文章数量:1356846

Note sure what's going on here...

I'm working with a datagridview in a Windows Forms application. I'm setting the DataSource as a table in a database (sample below) that has a BIT column. As expected (and desired), it is defining and displaying the column as a checkbox.

When updating the database, however, no matter whether the checkbox is checked or unchecked, the cell value returns null. I'm rather annoyed, but I know it's likely an obvious fix.

public class Connector 
{
    private static string ConnectionString { get; } = "Connection String";
    private SqlConnection Con { get; } = new SqlConnection(ConnectionString);
    public DataSet Data { get; set; }
  
    public Connector(string statement) 
    {
        Data = new DataSet();
  
        Con.Open();

        SqlDataAdapter Adapter = new SqlDataAdapter(statement, Con);
        Adapter.Fill(Data, "Table1");

        Con.Close();
    }
}

public partial class Form1 : Form 
{
    Connector connectorObj { get; set; }
 
    public Form1() 
    {
        // For reference, Table1 has the following columns: ID (INT), cName (VarChar),
        // Website (VarChar), and IsVendor (BIT)
        connectorObj = new Connector("SELECT * FROM Table1");

        InitializeComponent();
    }
  
    private void Form1_Load(object sender, EventArgs e) 
    {
        // This line seems to make the "BIT" column a CheckBox value, which is desired.
        // However, no matter whether it is checked or unchecked, it returns a null value
        dtaGrid.DataSource = connectorObj.Data.Tables[0];
    }
}

Edit - For reference, here is the code that I am using to test the value of the checkbox:

private void dtaContacts_RowLeave(object sender, DataGridViewCellEventArgs e)
{
    if (newContactIndex >= 0)
    {
        DataRow row = connectorObj.Data.Tables[0].NewRow();
        row[1] = dtaContacts.Rows[newContactIndex].Cells[1].Value;
        row[2] = dtaContacts.Rows[newContactIndex].Cells[2].Value;
        row[3] = dtaContacts.Rows[newContactIndex].Cells[3].Value.ToString().IsNullOrEmpty() ? true : false;
        
        connectorObj.UpdateDatabase();
        newContactIndex = -1;
    }
}

I have a Break Point on the connectorObj.UpdateDatabase(); line to see the row values before updating the database. The value of the checkbox column is always true here because IsNullOrEmpty() = true.

Note sure what's going on here...

I'm working with a datagridview in a Windows Forms application. I'm setting the DataSource as a table in a database (sample below) that has a BIT column. As expected (and desired), it is defining and displaying the column as a checkbox.

When updating the database, however, no matter whether the checkbox is checked or unchecked, the cell value returns null. I'm rather annoyed, but I know it's likely an obvious fix.

public class Connector 
{
    private static string ConnectionString { get; } = "Connection String";
    private SqlConnection Con { get; } = new SqlConnection(ConnectionString);
    public DataSet Data { get; set; }
  
    public Connector(string statement) 
    {
        Data = new DataSet();
  
        Con.Open();

        SqlDataAdapter Adapter = new SqlDataAdapter(statement, Con);
        Adapter.Fill(Data, "Table1");

        Con.Close();
    }
}

public partial class Form1 : Form 
{
    Connector connectorObj { get; set; }
 
    public Form1() 
    {
        // For reference, Table1 has the following columns: ID (INT), cName (VarChar),
        // Website (VarChar), and IsVendor (BIT)
        connectorObj = new Connector("SELECT * FROM Table1");

        InitializeComponent();
    }
  
    private void Form1_Load(object sender, EventArgs e) 
    {
        // This line seems to make the "BIT" column a CheckBox value, which is desired.
        // However, no matter whether it is checked or unchecked, it returns a null value
        dtaGrid.DataSource = connectorObj.Data.Tables[0];
    }
}

Edit - For reference, here is the code that I am using to test the value of the checkbox:

private void dtaContacts_RowLeave(object sender, DataGridViewCellEventArgs e)
{
    if (newContactIndex >= 0)
    {
        DataRow row = connectorObj.Data.Tables[0].NewRow();
        row[1] = dtaContacts.Rows[newContactIndex].Cells[1].Value;
        row[2] = dtaContacts.Rows[newContactIndex].Cells[2].Value;
        row[3] = dtaContacts.Rows[newContactIndex].Cells[3].Value.ToString().IsNullOrEmpty() ? true : false;
        
        connectorObj.UpdateDatabase();
        newContactIndex = -1;
    }
}

I have a Break Point on the connectorObj.UpdateDatabase(); line to see the row values before updating the database. The value of the checkbox column is always true here because IsNullOrEmpty() = true.

Share Improve this question edited Mar 31 at 9:33 Chris Brickner asked Mar 30 at 11:37 Chris BricknerChris Brickner 253 bronze badges 3
  • Just to clarify, you're checking/unchecking boxes in rows and expecting the change to be saved in the database but it's being saved as null in the db. Is that correct? – Sal Commented Mar 30 at 12:45
  • BIT is a three-state value, it can be null. Is that the case? Do you have null values in that Column? -- Since the question is related to the process of saving the data, you should show that part of the code. BTW, you should NOT store the Connection object, and when using a DataAdapter, you should not open and close the connection yourself, the DataAdapter does that on its own. You should only dispose of the Connection object after (declaring the Connection with a using statement) – Jimi Commented Mar 30 at 14:42
  • No, The Database is loading the form as a checkbox column. This is what I want, but no matter what I do WITH the checkbox, the value remains the same: NULL in the cell. When it goes to update the database, it sets it to NULL regardless of whether the box is checked or not. – Chris Brickner Commented Mar 31 at 9:04
Add a comment  | 

1 Answer 1

Reset to default 0

I got it... or, at least I understand what is happening.

When I leave the row while still in the cell, the cell is still dirty and has not updated. If on a new row (which I always was), the cell value starts as null. It seems to call dtaContacts_RowLeave() before changing the dirty state. Now I just need to force change the dirty state before calling RowLeave(). Thank you everyone for your assistance!

本文标签: cDataGridView checkbox column is always nullregardless of checked stateStack Overflow