We help IT Professionals succeed at work.
Get Started

Change column types in a datagridview prgrammaticaly while using a Linq query for the data.

1,273 Views
Last Modified: 2013-11-11
Hello Experts.

I am using a Linq query to get the data to bind to a datagridview as shown below, but the "Admins, Users, Ltd Users, New Users" columns are all boolean and I would like to have those columns be of checkbox type.  How do you choose the column type during runtime?

Thanks.
Dim dc As New clsPDAppsDataClassesDataContext
 
        '// Fill the Permissions DataGrdViews
        '//Global
        Dim qPermision = (From p In dc.GetTable(Of sys_Permission)() _
                      Order By p.Name Ascending _
                      Select p).ToList()
 
        With dgvGlobalPermissionsDataGridView
 
            .DataSource = qPermision
 
            .Columns.Item(0).ReadOnly = True
            .Columns.Item(0).HeaderText = "ID"
            .Columns.Item(0).Width = 50
            .Columns.Item(0).AutoSizeMode = DataGridViewAutoSizeColumnMode.None
            .Columns.Item(0).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
            .Columns.Item(0).Visible = False
 
            .Columns.Item(1).ReadOnly = True
            .Columns.Item(1).HeaderText = "Name"
            .Columns.Item(1).AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
            .Columns.Item(1).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleLeft
 
            .Columns.Item(2).ReadOnly = True
            .Columns.Item(2).HeaderText = "Admins"
            .Columns.Item(2).Width = 50
            .Columns.Item(2).AutoSizeMode = DataGridViewAutoSizeColumnMode.None
            .Columns.Item(2).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
 
            .Columns.Item(3).HeaderText = "Users"
            .Columns.Item(3).Width = 50
            .Columns.Item(3).AutoSizeMode = DataGridViewAutoSizeColumnMode.None
            .Columns.Item(3).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
 
            .Columns.Item(4).HeaderText = "Ltd Users"
            .Columns.Item(4).Width = 50
            .Columns.Item(4).AutoSizeMode = DataGridViewAutoSizeColumnMode.None
            .Columns.Item(4).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
 
            .Columns.Item(5).ReadOnly = True
            .Columns.Item(5).HeaderText = "New Users"
            .Columns.Item(5).Width = 50
            .Columns.Item(5).AutoSizeMode = DataGridViewAutoSizeColumnMode.None
            .Columns.Item(5).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
 
            .ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
            .AllowUserToResizeColumns = False
            .AllowUserToResizeRows = False
            .AllowUserToAddRows = False
            .RowHeadersVisible = False
            .AllowUserToOrderColumns = False
            .AllowUserToDeleteRows = False
            .ForeColor = Color.Black
            .BackgroundColor = Color.White
            .DefaultCellStyle.SelectionBackColor = Color.White
            .DefaultCellStyle.SelectionForeColor = Color.Black
            .ColumnHeadersDefaultCellStyle.SelectionForeColor = Color.Black
            .ColumnHeadersDefaultCellStyle.SelectionBackColor = Color.WhiteSmoke
            .ColumnHeadersDefaultCellStyle.ForeColor = Color.Black
            .ColumnHeadersDefaultCellStyle.BackColor = Color.WhiteSmoke
            .AlternatingRowsDefaultCellStyle.BackColor = Color.Cornsilk
            .AlternatingRowsDefaultCellStyle.SelectionBackColor = Color.Cornsilk
 
        End With
        '... End Global Permissions DataGridView

Open in new window

Comment
Watch Question
Commented:
This problem has been solved!
Unlock 1 Answer and 7 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE