none
Datagridview Checkbox Show value "F" as unchecked, "T" as checked RRS feed

  • Question

  • Hi folks,

    I have a datagridView on my form.

    One of the columns that is bound to either has "T" or "F". I want to use a DataGridViewCheckBoxColumn in the grid but when I try it,  I am getting error message:

    System.FormatException: F is not a valid value for Boolean...Makes senese I suppose

    Is there an easy way around this ?

    J


    jppnn

    • Moved by CoolDadTx Friday, November 8, 2019 1:51 PM Winforms related
    Thursday, November 7, 2019 8:26 PM

Answers

  • If you have a DataTable, then you can add a simple calculable column that is based on an expression like “Column1=’T’”. Then bind to this new column. Give some details about your data source.

    • Marked as answer by john pp nn Friday, November 8, 2019 11:37 AM
    Friday, November 8, 2019 5:36 AM

All replies

  • Use a case in your SQL e.g.

    SELECT id, CASE WHEN SomeField = 'T' THEN 1 WHEN SomeField = 'F' THEN 0 END FROM dbo.SomeTable;


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Thursday, November 7, 2019 8:57 PM
    Moderator
  • Hi John,
     
    I have a blog post that might help: 
     
    https://geek-goddess-bonnie.blogspot.com/2010/02/checkbox-class-bound-to-non-booleans.html
     
    In my post, I have sub-classed the CheckBox class and handled the Format and Parse events of the Binding. This might work the same for you if sub-class the DataGridViewCheckBox, but I haven't actually tried it, so no promises.
     
    Maybe you could give it a try and see if it works ...

    ~~Bonnie DeWitt [C# MVP]

    http://geek-goddess-bonnie.blogspot.com

    Friday, November 8, 2019 12:46 AM
  • If you have a DataTable, then you can add a simple calculable column that is based on an expression like “Column1=’T’”. Then bind to this new column. Give some details about your data source.

    • Marked as answer by john pp nn Friday, November 8, 2019 11:37 AM
    Friday, November 8, 2019 5:36 AM
  • Hi john pp nn, 

    Thank you for posting here.

    According to your question, I make a test on my side, and I convert ‘T’ to ‘true’ and ‘F’ to ‘false’ in my datatable.

    Here’s the code.

                string connString = @"your connect string ";
                string query = "select * from Table";
    
                SqlConnection conn = new SqlConnection(connString);
                SqlCommand cmd = new SqlCommand(query, conn);
                conn.Open();
    
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                DataTable dt = new DataTable();
                da.Fill(dt);
                conn.Close();
                da.Dispose();
    
                for (int i=0;i<dt.Rows.Count;i++)
                {
                    dt.Rows[i][1] = (dt.Rows[i][1].ToString() == "T") ? true : false;
                }
    
                dataGridView1.Columns[0].Name = "ID";
                dataGridView1.Columns[0].DataPropertyName = "ID";
                dataGridView1.Columns[1].Name = "Value";
                dataGridView1.Columns[1].DataPropertyName = "Value";
                dataGridView1.DataSource = dt;
    

    Data in my table.

    Result:

    Best Regards,

    Xingyu Zhao



    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, November 8, 2019 5:38 AM
  • THanks folks,

    I managed to figure it out in the end. I just added a new column and bound the checkbox to that.

    J


    jppnn

    Friday, November 8, 2019 11:38 AM