By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,061 Members | 1,539 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,061 IT Pros & Developers. It's quick & easy.

DataGrid with checkboxes

P: n/a
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

Nov 21 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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!

Nov 21 '05 #2

P: n/a
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
GatorBait wrote:
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

Nov 21 '05 #3

P: n/a
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...

Nov 21 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.