VB.net - DataGridView CheckBox Columns

Jimbo99999
Jimbo99999 used Ask the Experts™
on
Good Afternoon Experts!

I have a task that I am not quite sure how to accomplish in the code.  I have a DataGridView that I am loading from a SQL database query.  The records returned will have the first 3 values as regular columns in the DataGridView.  The 4th value in the record I need to add a column to the DataGridView as a checkbox and make the heading of the column the actual value.  Then each subsequent record that is in the resulting records of the SQL query I don't need to load a new line in the DataGridView, I just need to make a new checkbox column for the 4th value with the actual value as the column header.  

So, I will have 1 row in the DataGridView with the first 3 values being standard columns. Then columns 4 on need to be dynamically added in code based on number of results returned form the SQL query.  

Username       First      Last       Can Edit     Can Update   Can Add
BobH                Bob       H             (chk)          (chk)              (chk)

Any ideas where I can start?

Thansk,
jimbo99999
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2012
Top Expert 2014

Commented:
So datagridview will only contain one row?
Commented:
suppose that the incoming datatable from database is declared as "l_ActualTable" , then do the following.

1- Clone the structure of the original table
2- Change the datatype of the columns to boolean, so when we add them to the gridview they will atomatically change to check box. I started the below loop from i= 3 becuase you asked for it to be > 3
3- reload the original datarows into the new datatable. We did that because you cannot change the column type of a table when it has data in it.
N.B. considering that the column ought to be converted do not contain data, or contains a valid boolean (true/false), or (1/0).

    l_SecondaryTable = l_ActualTable.Clone 'Clone Structure of the original table
    For i As Integer = 3 To l_SecondaryTable.Columns.Count - 1
      l_SecondaryTable.Columns(i).DataType = GetType(Boolean)  
    Next

    For Each l_drow As DataRow In l_ActualTable.Rows
      l_SecondaryTable.ImportRow(l_drow)
    Next
    ds.Tables.Add(l_SecondaryTable)
    DataGridView1.DataSource = ds
    DataGridView1.DataMember = "table"

Open in new window

Author

Commented:
The requirements have changed for this part of the project which will be a design change for this functionality.  I will have to make a new question if I cannot find a solution.

I still wanted to award points for the responses since I added them to my knowledge base for future consideration/reference.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial