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.
1 Answer
Reset to default 0I 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!
null
in the db. Is that correct? – Sal Commented Mar 30 at 12:45using
statement) – Jimi Commented Mar 30 at 14:42