-
DataGrid column widths
Hi
Im having a bit of trouble with the DataGrid control.
I have a Dataset that gets populated by the contents of an Access Database file.
I then bind the DataGrid control to the dataset so the contents are displayed.
Now im having trouble sizing the columns to fit the data that gets displayed there.
I would like the columns to automatically resize themselfs to fit the data correctly. Current im using
Code:
.PreferredColumnWidth = 150
but this makes the columns much to big for small fields, primary key for exmaple.
-
I think you need to add a custom table style to the datagrid. Try looking into this - sorry I don't have any code - just what I remember.
-
DataGrid column widths
You can to use a TableStyle and add it to the DataGrid Tablestyles property.
For example
Dim dgStyle as DataGridTableStyle
Dim dgTableStyle As New DataGridTableStyle()
Me.DataGrid1.TableStyles.Clear()
Dim colStyle1 As New DataGridTextBoxColumn()
With colStyle1
.Alignment = HorizontalAlignment.Left
.HeaderText = "Name"
.MappingName = "EmployeeName"
.Width = 50
End With
dgTableStyle.GridColumnStyles.Add(colStyle1)
' This must be the table name or the format
' will not apply to the columns.
dgTableStyle.MappingName = "tblEmployees"
Me.DataGrid1.TableStyles.Add(dgTableStyle)