Results 1 to 13 of 13

Thread: [RESOLVED] DataGridViewCheckBoxColumn from SQL bit type

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jul 2013
    Posts
    178

    Resolved [RESOLVED] DataGridViewCheckBoxColumn from SQL bit type

    I am looking for a method to convert a SQL bit column (0, 1, NULL) queried into a DataGridViewCheckBoxColumn without using a loop. I tried:

    VB.NET Code:
    1. Dim cell As DataGridViewCell = New DataGridViewCheckBoxCell()
    2.  
    3.         With DataGridView2
    4.             With .Columns("FLY")
    5.                 .CellTemplate = cell
    6.             End With
    7.         End With

    which resulted in the error: "An unhandled exception of type 'System.InvalidCastException' occurred in System.Windows.Forms.dll. Additional information: Value provided for CellTemplate must be of type System.Windows.Forms.DataGridViewTextBoxCell or derive from it."

    I believe this has to do with the 3 state value of the bit type in the SQL column (0, 1, NULL). I also thought maybe to CAST the SQL value to a text value, but wasn't sure of the syntax. What is the best way to go about this and avoid the loop? Perhaps if the data only had 0,1 then VB.net would automatically convert this to TRUE/FALSE and display the Checkbox in the DataGridView?
    Last edited by Fedaykin; Sep 14th, 2020 at 12:14 PM.

  2. #2
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Posts
    12,371

    Re: DataGridViewCheckBoxColumn from SQL bit type

    The column needs to be declared as a DataGridViewCheckBoxColumn and it also needs to specify that its ThreeState property is true. How is the DataGridView initially created (e.g. via the designer or via code somewhere else)?
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | HtmlLessons | CssLessons | Code Tags | Sword of Fury - Jameram

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Jul 2013
    Posts
    178

    Re: DataGridViewCheckBoxColumn from SQL bit type

    Thank you for the quick reply. The DataGridView is created in Designer and populated with an SQL query. I was attempting to declare the DataGridViewCheckBoxColumn with code above but I need the data loaded before I can identify the "FLY" column.

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Jul 2013
    Posts
    178

    Re: DataGridViewCheckBoxColumn from SQL bit type

    Okay, I tried:

    VB.NET Code:
    1. Dim colu As DataGridViewCheckBoxColumn = DataGridView2.Columns("FLY")
    2.         With colu
    3.             .ThreeState = True
    4.         End With

    And got:

    An unhandled exception of type 'System.InvalidCastException' occurred in pgr.exe

    Additional information: Unable to cast object of type 'System.Windows.Forms.DataGridViewTextBoxColumn' to type 'System.Windows.Forms.DataGridViewCheckBoxColumn'.

  5. #5
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Posts
    12,371

    Re: DataGridViewCheckBoxColumn from SQL bit type

    So to clarify, the DataGridView is created in the designer but the DataGridViewColumns are not? E.g. you're relying on the AutoGenerateColumns property to let the DataGridView automatically create the respective columns?
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | HtmlLessons | CssLessons | Code Tags | Sword of Fury - Jameram

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Jul 2013
    Posts
    178

    Re: DataGridViewCheckBoxColumn from SQL bit type

    Your assumptions are correct, DataGridView is created in Designer and DataGridViewColumns are autogenerated. I think the NULL values in the database bit column are what are killing me. Maybe if I updated all of the NULLs to 0 then the column will auto-generate as a CheckBoxColumn?

  7. #7
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Posts
    12,371

    Re: DataGridViewCheckBoxColumn from SQL bit type

    One option that you have is to do the following after the data is populated and the columns are automatically generated:
    1. Get the index of the Fly column in relation to the DataGridView's columns
    2. Create a new DataGridViewCheckBoxColumn using the same properties of the Fly column
    3. Specify the ThreeState property of the newly created DataGridViewCheckBoxColumn
    4. Remove the Fly column by its index
    5. Insert the newly created DataGridViewCheckBoxColumn at the same index where Fly was
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | HtmlLessons | CssLessons | Code Tags | Sword of Fury - Jameram

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Jul 2013
    Posts
    178

    Re: DataGridViewCheckBoxColumn from SQL bit type

    Quote Originally Posted by dday9 View Post
    One option that you have is to do the following after the data is populated and the columns are automatically generated:
    1. Get the index of the Fly column in relation to the DataGridView's columns
    2. Create a new DataGridViewCheckBoxColumn using the same properties of the Fly column
    3. Specify the ThreeState property of the newly created DataGridViewCheckBoxColumn
    4. Remove the Fly column by its index
    5. Insert the newly created DataGridViewCheckBoxColumn at the same index where Fly was
    Okay, but how do I get the data into the new column without looping? Is that possible?

  9. #9
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Posts
    12,371

    Re: DataGridViewCheckBoxColumn from SQL bit type

    Yeah, if you specify the DataPropertyName of the newly created DataGridViewCheckBoxColumn it will show the data of the Fly field. That's part of step 2: ... using the same properties of the Fly column
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | HtmlLessons | CssLessons | Code Tags | Sword of Fury - Jameram

  10. #10

    Thread Starter
    Addicted Member
    Join Date
    Jul 2013
    Posts
    178

    Re: DataGridViewCheckBoxColumn from SQL bit type

    Ahhhh, gotcha. Okay let me try.

  11. #11

    Thread Starter
    Addicted Member
    Join Date
    Jul 2013
    Posts
    178

    Re: DataGridViewCheckBoxColumn from SQL bit type

    Okay that works!? It makes zero sense to me how it works, but it works. Here is the working code:

    VB.NET Code:
    1. 'after the DataGridView datasource is set:
    2.  
    3.         Dim FlyIndex As Integer = DataGridView2.Columns.Item("FLY").Index
    4.         DataGridView2.Columns.RemoveAt(FlyIndex)
    5.  
    6.         Dim PFC As New DataGridViewCheckBoxColumn
    7.         PFC.HeaderText = "FLY"
    8.         PFC.Name = "FLY"
    9.         PFC.DataPropertyName = "FLY"
    10.         PFC.ThreeState = True
    11.         DataGridView2.Columns.Insert(FlyIndex, PFC)

    Somehow this magically remembers the data for the column that was removed just by naming it the same?? What sorcery is this? If we remove the column it is 'gone', but obviously its held in memory somehow. By the way, I tried only setting PFC.HeaderText and PFC.Name without setting the PFC.DataPropertyName and this still worked. I just went ahead and set the DataPropertyName because that's what you said to do. Somehow this seems unclean to me but I'll take it until I educate myself more about it.

    Thank you very much!

  12. #12
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: [RESOLVED] DataGridViewCheckBoxColumn from SQL bit type

    The data is remembered because it not stored in the grid columns, it is stored in a DataTable (via the DataSource property of the grid). The grid just displays the relevant parts of the DataTable when they are on screen.

    The DataPropertyName specifies what column of the data (in the DataSource) the column should display... in basic theory it shouldn't work without setting that, but I assume that if it is not set then the Name property gets tried instead - which in this case happens to have the same value, so it works. It is best to specify DataPropertyName tho, as the behaviour you've seen might not be guaranteed.

  13. #13

    Thread Starter
    Addicted Member
    Join Date
    Jul 2013
    Posts
    178

    Re: [RESOLVED] DataGridViewCheckBoxColumn from SQL bit type

    Oh my goodness, of course! We are removing only the displayed column, not the datatable column! Thank you, I'm not sure why I didn't connect that dot. I agree that it probably defaults to the .Name property, but better to set the .DataPropertyName. Outstanding, thank you everyone for your input. I could not find this solution anywhere else.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width