locked
switch textbox column to checkbox column RRS feed

  • Question

  • Does anyone know how to switch textbox column to checkbox column in datagridview?
    I mean I have a column with values: 1,0  and I want to switch them to a checkbox...

    thanks a lot!
    Tuesday, June 30, 2009 5:00 PM

Answers

  • Another option is to turn off the automatic generation of the columns and do it yourself. Something like this:

                // turn off automatic generation of the columns
                CustomersDataGridView.AutoGenerateColumns = false;
    
                // Build the columns
                DataGridViewTextBoxColumn dcTextBox = new DataGridViewTextBoxColumn()
                {
                    Name = "CustomerNameColumn",
                    HeaderText = "CustomerName",
                    DataPropertyName = "FullName"
                };
                CustomersDataGridView.Columns.Add(dcTextBox);
    
                DataGridViewCheckBoxColumn dcCheckBox = new DataGridViewCheckBoxColumn()
                {
                    Name = "SomeBooleanValue",
                    HeaderText = "Current?",
                    DataPropertyName = "IsCurrent"
                };
                CustomersDataGridView.Columns.Add(dcCheckBox);
    
                CustomersDataGridView.DataSource = Customers.RetrieveAsDataTable();
    Hope this helps.
    www.insteptech.com
    We are volunteers and ask only that if we are able to help you, that you mark our reply as your answer. THANKS!
    • Proposed as answer by Aland Li Saturday, July 4, 2009 2:16 AM
    • Marked as answer by SuperMath Monday, July 6, 2009 7:31 AM
    Tuesday, June 30, 2009 6:51 PM
  • Hi SuperMath,

    You can add a checkbox column by following the code snippet below:
    DataGridViewCheckBoxColumn checkCol = new DataGridViewCheckBoxColumn();

    checkCol.Name = "isMan";

    checkCol.HeaderText = "isMan";           

    checkCol.TrueValue = 1;

    checkCol.FalseValue = 0;

    checkCol.DefaultCellStyle.NullValue = 0;

    checkCol.DataPropertyName = "isMan";

    this.dataGridView1.Columns.Add(checkCol);

     

    Please pay attention to the DataPropertyName property, its value should be the column name in you DataTable.

     

    Let me know if this helps.
    Aland Li


    Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
    • Marked as answer by SuperMath Monday, July 6, 2009 7:31 AM
    Friday, July 3, 2009 4:18 PM

All replies

  • At runtime or design time?

    At design time, you can use the smart tag of the DataGridView to set the columns.

    Hope this helps.
    www.insteptech.com
    We are volunteers and ask only that if we are able to help you, that you mark our reply as your answer. THANKS!
    Tuesday, June 30, 2009 5:07 PM
  • I will explain, I read the data from a table (sql command) and with dataset I put all the data in the datagridview, so I can't change the design because, I don't have any access to the columns until the dataset get the data...
    so, I don't know how to change the column when I get the data from the DataSet

    I hope you understand me.
    thanks for the quick answer!

    Tuesday, June 30, 2009 5:15 PM
  • Could you post the code you use to bind the DataGridView?
    www.insteptech.com
    We are volunteers and ask only that if we are able to help you, that you mark our reply as your answer. THANKS!
    Tuesday, June 30, 2009 5:31 PM
  • here:
    MyDatabase db;
    db = new MyDatabase();
                
    DataSet ds = new DataSet();
                
     // show table
    OracleDataAdapter da = db.executeQueryToDataSet("SELECT * FROM PIZZAORDER");
    da.Fill(ds);
    dgv.DataSource = ds.Tables[0]; // dgv is the dataGridView         
    //in the class MyDatabase there is a function: //db.executeQueryToDataSet public OracleDataAdapter executeQueryToDataSet(string sql) { // con is the name of the connection. con.Open(); OracleCommand command = con.CreateCommand(); command.CommandText = sql; OracleDataAdapter da = new OracleDataAdapter(command); return da; }

    Tuesday, June 30, 2009 5:43 PM
  • Do you have any control over the database? If so, you can change the column in the underlying database to a boolean value (it is bit in SQL Server, I don't know what it is in Oracle). If you do this, it should come up as a checkBox column automatically.

    Let me know if this is possible for you.


    www.insteptech.com
    We are volunteers and ask only that if we are able to help you, that you mark our reply as your answer. THANKS!
    Tuesday, June 30, 2009 5:59 PM
  • I tried to change it to boolean before but I didn't see any boolean in the types , i'm working with oracle sqlDeveloper if it helps...
    Tuesday, June 30, 2009 6:30 PM
  • I have not used Oracle in about 7 years. So I don't recall. But in SQL Server, it is "bit".
    www.insteptech.com
    We are volunteers and ask only that if we are able to help you, that you mark our reply as your answer. THANKS!
    Tuesday, June 30, 2009 6:35 PM
  • Another option is to turn off the automatic generation of the columns and do it yourself. Something like this:

                // turn off automatic generation of the columns
                CustomersDataGridView.AutoGenerateColumns = false;
    
                // Build the columns
                DataGridViewTextBoxColumn dcTextBox = new DataGridViewTextBoxColumn()
                {
                    Name = "CustomerNameColumn",
                    HeaderText = "CustomerName",
                    DataPropertyName = "FullName"
                };
                CustomersDataGridView.Columns.Add(dcTextBox);
    
                DataGridViewCheckBoxColumn dcCheckBox = new DataGridViewCheckBoxColumn()
                {
                    Name = "SomeBooleanValue",
                    HeaderText = "Current?",
                    DataPropertyName = "IsCurrent"
                };
                CustomersDataGridView.Columns.Add(dcCheckBox);
    
                CustomersDataGridView.DataSource = Customers.RetrieveAsDataTable();
    Hope this helps.
    www.insteptech.com
    We are volunteers and ask only that if we are able to help you, that you mark our reply as your answer. THANKS!
    • Proposed as answer by Aland Li Saturday, July 4, 2009 2:16 AM
    • Marked as answer by SuperMath Monday, July 6, 2009 7:31 AM
    Tuesday, June 30, 2009 6:51 PM
  • I didn't find somthing like that...:(

    EDIT: 
    I found this:

    DataGridViewCheckBoxColumn myCheck = new DataGridViewCheckBoxColumn();
    myCheck.Name = "undefined";


    and I added this column to my table and it worked, but I didn't manage to check them... (they are in a false state)
    do you hav any idea?

    thanks

    EDIT: what is the Customers in:

    Customers.RetrieveAsDataTable();     ?
    ?
    • Edited by SuperMath Tuesday, June 30, 2009 8:18 PM
    Tuesday, June 30, 2009 7:10 PM
  • When you said you "didn't find something like that", what *that* are you referring to?

    My code is manually adding columns to the DataGridView. You said that your code (which is creating a DataGridViewCheckBoxColumn) added a column to your table?

    Did you notice the DataPropertyName property that I set for the CheckBoxColumn? This is how the DataGridView maps the columns in your DataTable to the columns in your DataGridView. If that is not set properly, you would get the data showing up in the column.

    Maybe it would be helpful for you to post the code you used instead of the code I posted?

    The Customers.RetrieveAsDataTable in my example is doing just as it says, it is retrieving a DataTable. You could just replace that code with yours:

    dgv.DataSource = ds.Tables[0]; 
    Hope this helps.
    www.insteptech.com
    We are volunteers and ask only that if we are able to help you, that you mark our reply as your answer. THANKS!
    • Proposed as answer by Aland Li Saturday, July 4, 2009 2:24 AM
    Tuesday, June 30, 2009 9:24 PM
  • "didn't find something like that" - I ment to "bit" or "boolean".

    when I created a DataGridViewCheckBoxColumn to the table, and I added it to my table - it worked and I could see the new column with Unchecked checkBoxes, but when I tried to checked some of them, they didn't responed and they stayed unchecked.

    Do you have any idea how to checked them? because I have only the option: dgv.Rows[i].Cells[dgv.Columns.Count - 1].Value = true;
    but usually when I work with checkBox, I have the option ".checked"   and not ".value" .

    I thought on another option but I don't know if it's possible, is there an option to change the DataSet data? (I mean change it from 0 to false and 1 -> true)


    now when I ran your code I got a table with 2 columns (the first is textbox and the second is checkbox).

    this is my code:
    db = new MyDatabase();
    DataSet ds = new DataSet();

    // show table
    OracleDataAdapter da = db.executeQueryToDataSet("SELECT * FROM PIZZAORDER");
    da.Fill(ds);


    DataGridViewCheckBoxColumn myCheck = new DataGridViewCheckBoxColumn();
    myCheck.DataPropertyName = "undefined";
    myCheck.Name = "undefined";

    dgv.Columns.Add(myCheck);

    dgv.DataSource = ds.Tables[0];
    db.CloseConnection();


    // this command doesn't change anytuhing
    dgv.Rows[0].Cells["undefined"].Value = true;
    thanks for your help


    Tuesday, June 30, 2009 10:59 PM
  • Hmmm. It worked for me. Using my DataGridView name, I did this:

                CustomersDataGridView.Rows[0].Cells["IsCurrent"].Value = true;
    And it set the checked the column in the grid.


    www.insteptech.com
    We are volunteers and ask only that if we are able to help you, that you mark our reply as your answer. THANKS!
    Tuesday, June 30, 2009 11:10 PM
  • I tried what you said and it worked, I got a table with one row and the checkbox waschecked, but when I added again my dataset, I got 7 rows and none of them was checked...
    Tuesday, June 30, 2009 11:31 PM
  • So when you look at your dataTable, it has a field named "undefined"?

    What are the values for that field? Are they 0/1, True/False, or what?
    www.insteptech.com
    We are volunteers and ask only that if we are able to help you, that you mark our reply as your answer. THANKS!
    Wednesday, July 1, 2009 2:47 PM
  • In my DataGridView I see a column of checkBoxes and the value of all the checkBoxes is false. (I mean all of them are unchecked)
    Wednesday, July 1, 2009 9:12 PM
  • No. I mean the actual DataTable.

    If you hover over the ds variable at runtime and look at the DataTable using the DataTable visualizer, do you have a column in the TABLE for undefined? What are the values for that field?
    www.insteptech.com
    We are volunteers and ask only that if we are able to help you, that you mark our reply as your answer. THANKS!
    Wednesday, July 1, 2009 9:20 PM
  • I didn't understand what did you mean in: "DataTable"how can I get to it?
    Wednesday, July 1, 2009 11:51 PM
  • THIS:

    ds.Tables[0];

    Is a DataTable. A DataSet is comprised of datatables.

    To view the contents of your database, set a break point on any line after the da.Fill line.

    Run the app until you stop at the break point.

    Hover over your ds variable. You should get a tooltip that displays a magnifying glass somewhere in the middle of it.

    Hover over the magnifying glass and it should give you a "DataSet visualizer" menu option.

    Pick it.

    It will then show you *exactly* what the DataSet and the DataTables within the DataSet look like.

    Hope this helps.
    www.insteptech.com
    We are volunteers and ask only that if we are able to help you, that you mark our reply as your answer. THANKS!
    Thursday, July 2, 2009 12:02 AM
  • Here is a picture of what I am describing:

    http://www.devx.com/codemag/Article/27821/1763?supportItem=5
    www.insteptech.com
    We are volunteers and ask only that if we are able to help you, that you mark our reply as your answer. THANKS!
    Thursday, July 2, 2009 12:08 AM
  • wow, it's a very useful tool! (by the way: your explanation was great, I understood it also without the link...)
    however, undifined isn't shown in the DataTable, and that didn't suprise me, because the column doesn't appear in the oracle table, I created the column and added it to the datagridview manually, without the dataSet.
    I did it because I thought on a way: create a CheckBox column, copy the other column to it (1->true, 0->false), and make the source column unvisible.
    Thursday, July 2, 2009 12:38 AM
  • Yea, it is a great tool. The data here is updatable, so you can update your test data while you're debugging if you need to.

    This explains why this is not working.

    Do you have code somewhere that is copying the column data to this new column you created? If so, could you post it?


    www.insteptech.com
    We are volunteers and ask only that if we are able to help you, that you mark our reply as your answer. THANKS!
    Thursday, July 2, 2009 12:52 AM
  • Hi SuperMath,

     

    Based on your description, the main problem is that the data type of the DataGridViewCheckBoxColumn cannot be mapped to bool. You said that the data is 1 or 0, so the data type ought to be integer. Please feel free to tell me if I misunderstood you.

     

    From my experience, the DataGridViewCheckBoxColumn support binding other types of data, such as string or int. But we need to set the values of some properties to achieve the goal. The properties are TrueValue, FalseValue, and NullValue. We also need to handle the DataError event of the DataGridView, because binding other type of data can throw a FormatException. This is my code snippet:

    Add the column:
    DataGridViewCheckBoxColumn checkCol = new DataGridViewCheckBoxColumn();

    checkCol.Name = "isMan";

    checkCol.HeaderText = "isMan";           

    checkCol.TrueValue = 1;

    checkCol.FalseValue = 0;

    checkCol.DefaultCellStyle.NullValue = 0;

    checkCol.DataPropertyName = "isMan";

    this.dataGridView1.Columns.Add(checkCol);

    Handle the DataError event:
    private void dataGridView1_DataError(object sender, DataGridViewDataErrorEventArgs e)

    {

        if (e.Exception is FormatException && this.dataGridView1.Columns[e.ColumnIndex] is DataGridViewCheckBoxColumn)

            return;

        else

            MessageBox.Show(e.Exception.Message);

    }

     

    Let me know if this helps.
    Aland Li


    Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
    Thursday, July 2, 2009 8:25 AM
  • Deborahk, here is my code:
    int i;
    for (i = 0; i < dgv.Rows.Count - 1; i++)
    {
          if(dgv.Rows[i].Cells["TAKEAWAY"].Value.ToString() == "1")
                  dgv.Rows[i].Cells["undefined"].Value = true;
         else
                 dgv.Rows[i].Cells["undefined"].Value = false;
    }
    Aland Li, I think that the code that you post will work if I will be able to change it in the dataSet, otherwise I think that adding a column and using the code above will be easier...  
    do you know how to edit the DataSet? or maybe I didn't understand you well....

    Thanks.



    Thursday, July 2, 2009 12:33 PM
  • Hi SuperMath,

     

    I am sorry for not explaining my idea clearly. Based on my understanding, we do not need to change the DataSet, we only need to change the mapping relation. The default mapping is true->checked and false->unchecked, but this can be changed. In your case, the mapping is 1->checked and 0->unchecked. My code snippet in the last reply shows how to build this mapping relation. The results would be that if the value of a cell is 1, the check box would be checked, if the value is 0, the check box would be unchecked.

     

    From your reply, you wanted to modify the DataSet to achieve your goal. This is a good way, but it may cause some problems. For example, if the data type of the column is integer, but you would like to change a value from 1 to true. This will cause a data type error, because true is of type Boolean but the column is of type integer.

     

    Please feel free to tell me if my answer doesn’t meet your issue.
    Aland Li

     


    Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
    • Edited by Aland Li Thursday, July 2, 2009 3:29 PM
    Thursday, July 2, 2009 3:27 PM
  • So, how can I change the column from text to checkBox? because now I only have textboxes columns...
    Friday, July 3, 2009 2:09 PM
  • Hi SuperMath,

    You can add a checkbox column by following the code snippet below:
    DataGridViewCheckBoxColumn checkCol = new DataGridViewCheckBoxColumn();

    checkCol.Name = "isMan";

    checkCol.HeaderText = "isMan";           

    checkCol.TrueValue = 1;

    checkCol.FalseValue = 0;

    checkCol.DefaultCellStyle.NullValue = 0;

    checkCol.DataPropertyName = "isMan";

    this.dataGridView1.Columns.Add(checkCol);

     

    Please pay attention to the DataPropertyName property, its value should be the column name in you DataTable.

     

    Let me know if this helps.
    Aland Li


    Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
    • Marked as answer by SuperMath Monday, July 6, 2009 7:31 AM
    Friday, July 3, 2009 4:18 PM
  • Hi SuperMath,

     

    Please pay attention to the Deborahk’s reply which was posted at Tuesday, June 30, 2009 6:51 PM. That is a good suggestion, my replies just explained how to add the checkbox column in detail.

     

    Best regards,
    Aland Li


    Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
    Saturday, July 4, 2009 2:22 AM
  • First of all, thanks to both of you it worked!
    secondly, after changing the column, it moved and become the first column, how can I change the order of the columns in the datagridview?

    thanks.
    Saturday, July 4, 2009 6:55 PM
  • Hi SuperMath,

    From your description, you added some columns automatically by set the data source and added the checkbox column manually. If we want to control the sequences of all the columns, we need to follow DeborahK's advice, which means adding all the columns manually. Please pay attention to this code snippet:
                // turn off automatic generation of the columns
                CustomersDataGridView.AutoGenerateColumns = false;

    Best regards,
    Aland Li

    Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
    Monday, July 6, 2009 2:00 AM
  • it worked great (like always) :-)
    thanks a lot, Deborahk and Aland Li.
    Monday, July 6, 2009 10:27 AM