DataGrid with checkboxes

G

GatorBait

Hi all,

I'm using a datagrid for the first time and I am running into some
problems that I hope someone can help me with. I have a datagrid with
18 rows and 5 columns....column 1 is just text and columns
2-5 are checkboxes. Some of the checkboxes have to be invisible, and
in some cases some of the checkboxes have be be enabled=false. I have
it now so that the entire grid is populated correctly, but I am having
an extremely difficult time figuring out how to do the hiding and
disabling of the checkboxes! I will post the code I am using to
populate the datagrid, and if someone could please help me out by
posting code to hide and disable a checkbox (or even the whole cell) i
would be very grateful. Thank you so much in advance for your help!!!


(dgPermissions is the name of my DataGrid)
Dim dtPermissions As New DataTable("Permissions")

dtPermissions.Columns.Add("Item",
System.Type.GetType("System.String"))

For i = 1 To iNbrCols
dtPermissions.Columns.Add("ColumnName",
System.Type.GetType("System.Boolean"))
Next i

For J = 1 To iNbrRows
dgPermissions.RowTemplate.Height = 18
dgPermissions.RowTemplate.Resizable =
DataGridViewTriState.False
dtPermissions.Rows.Add(New Object() {"Text", False, False,
False, False})
dtPermissions.AcceptChanges()
Next J

dgPermissions.DataSource = dtPermissions
 
G

GatorBait

Sorry, I meant to replace iNbrCols with 4 and iNbrRows with 18.....they
are variables that are set somewhere else in the program, can be
hardcoded here for this example!
 
T

tomb

This may seem a little convoluted, but it's the way you have to do it:
Dim tStyle As Windows.Forms.DataGridTableStyle
Dim bCol As Windows.Forms.DataGridBoolColumn
Dim tCol As Windows.Forms.DataGridTextBoxColumn

dgPermissions.DataSource = ds.Tables("Permissions")
tStyle = New Windows.Forms.DataGridTableStyle
tStyle.MappingName = "Permissions"

tCol = New Windows.Forms.DataGridTextBoxColumn
tCol.Width = 105
tCol.NullText = ""
tCol.HeaderText = "Item"
tCol.MappingName = "Item"
tCol.ReadOnly = False
tStyle.GridColumnStyles.Add(tCol)

bCol = New Windows.Forms.DataGridBoolColumn
bCol.Width = 75
bCol.AllowNull = False
bCol.ReadOnly = True
tStyle.GridColumnStyles.Add(bCol)

dgPermissions.TableStyles.Add(tStyle)

Of course, you'll have to add however many boolean columns you want. If you don't want to even see one in the datagrid, just leave it out. If you want the user to be able to change the value, then make readonly=true.

Tom
 
G

GatorBait

Thank you so much Tom!

I tried using the code you provided and when I tried to just add the
columns, it didn't work. I only get a blank DataGrid...do you know
what would cause that?

Also, I'm not so sure I understand fully....once I get the grid set up,
how do I then enable/disable a checkbox?

Thank you again...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top