0

I have a datagridview in windows control form where I insert a checkbox column beside a data table queried from MYSQL database

private void ControlForm_Load(object sender, EventArgs e)
{
        DataGridViewCheckBoxColumn ChkboxCol = new DataGridViewCheckBoxColumn();
        ChkboxCol.HeaderText = "Checkbox";
        ChkboxCol.Width = 50;
        ChkboxCol.Name = "Checkbox";
        ChkboxCol.TrueValue = true;
        ChkboxCol.FalseValue = false;
        dataGridView1.Columns.Insert(0, ChkboxCol);
        int ColCount = dataGridView1.ColumnCount;
        dataGridView1.DataSource = tablequery();
}

public DataTable tablequery()
    {
        MY_DB DB = new MY_DB();

        string query = "SELECT * FROM sometable";
        MySqlCommand cmd = new MySqlCommand(query, DB.GetConnection);
        MySqlDataAdapter adapter = new MySqlDataAdapter();
        DataTable table = new DataTable();
        adapter.SelectCommand = cmd;
        adapter.Fill(table);

        return table;
    }

In the "sometable" table in MySQL, I have set the data type to be TINYINT (which arise after I select Boolean) in the "Checkbox_Value" column and I have set the number to be 0 or 1. I would want my results on the checkbox column of my datagrid to reflect as checked if the "Check_Value" of a particular entry to be 1 or unchecked if the "Check_value" of another particular entry is 0. The number of entries in the "sometable" and the value of the "Check_Value" would change and I wish this checkbox could reflect the checked status dynamically.

Thank you very much for any suggestion or advice.

I have included the table of my DB

enter image description here

where the column "XXXX" is the set_active column

XCY
  • 1
  • 2
  • In Mysql you could not set a Boolean data type directly. So a 0 would be false and non-zero would be true. https://stackoverflow.com/questions/289727/which-mysql-data-type-to-use-for-storing-boolean-values – XCY Dec 06 '20 at 05:08
  • Have you tried to bind the grid’s check box column to the “Check_Value” column in the `DataTable` by setting the grid’s check box column’s `DataPropertyName`? like… `ChkboxCol.DataPropertyName = “Check_Value”`? Does that give an error? Can you try this? Can you change the column type in MySQL to a `Bit`? – JohnG Dec 06 '20 at 05:18
  • In my test using MS SQL Server, binding the check box column to the “TinyInt” column returned from the DB worked. The check box will be checked for any values that are greater than 0 and unchecked when the value is 0. This should work, however, it should be noted, that if the original value in the `TinyInt` DB column is greater than 1, then, if this is “changed” by the user in the grid by “unchecking”, the check box for that cell, then the original value will be lost. – JohnG Dec 06 '20 at 06:01
  • Thanks JohnG for the suggestion. I am sorry that I am a newbie in C#. Could you give me a simple writeup on the code for the binding to work? Also my understanding is .NetFrame work does not require data binding as the datagridview would bind automatically. Please correct me if I am wrong. – XCY Dec 06 '20 at 07:29
  • You are correct that the _”datagridview would bind automatically”_ … when the column in the DB is a `BIT` or `Boolean` column, the grid will “automatically” make that column a `DataGridViewCheckBoxColumn`. When the column in the DB is a `TinyInt` as is show in the picture, then, the grid will NOT “automatically” set that column as a check box column in the grid. This makes sense because the `TinyInt` column can have values from 1-255. However, as I already noted… if you bind a “manually added check box column” to the grid, then, the `TinyInt` column will work as a check box column in the grid. – JohnG Dec 08 '20 at 02:25
  • Obviously, there is more to this than you are showing. It will help YOU if you provide all the necessary details to your question. I suggest you peruse the SO [Tour](https://stackoverflow.com/tour) section as it shows how SO works. The [How do I ask a good question?](https://stackoverflow.com/help/how-to-ask) may help. In addition, you may find the SO [Asking](https://stackoverflow.com/help/asking) section useful. Good Luck. – JohnG Dec 08 '20 at 02:26

0 Answers0