The Question:
I have a form with datagrid on it, but when i fill the grid with a table, the columns do not take up the entire space that is available in the datagrid. Also the individual column sizes do not show all the data in the field, i have to resize it using drag feature, but i have the space on the form when i first load it and i want the columns to take up all the space available in the datagrid.
The accepted answer by Riquel Dong – MSFT:
Based on your post, you need to change the width of the row or column in a DataGrid. You can use the DataGridTableStyle class to implement this.
it represents the table drawn by the System.Windows.Forms.DataGrid control at run time. Its property GridColumnStyles allows you to create a customized set of column styles. Pleaser read this article Advanced DataGrid Sizing for this problem. It shoulds help your to handle this problem. Here is the code exmaple about sizing the datagrid control. Hope this helps.
Public Class Form5
Dim dt As DataTable
Private Sub Form5_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
DataGrid1.Width = 450
dt = New DataTable
dt.Columns.Add("id", GetType(Integer))
dt.Columns.Add("name", GetType(String))
dt.Columns.Add("region", GetType(String))
For i As Integer = 0 To 5
dt.Rows.Add(i, "name " & i.ToString, "region " & i.ToString)
Next
Dim style As DataGridTableStyle = New DataGridTableStyle()
style.MappingName = dt.TableName
For Each column As DataColumn In dt.Columns
Dim gridColumn As DataGridTextBoxColumn = New DataGridTextBoxColumn()
gridColumn.MappingName = column.ColumnName
gridColumn.HeaderText = column.ColumnName
gridColumn.Width = 135
style.GridColumnStyles.Add(gridColumn)
Next
DataGrid1.TableStyles.Clear()
DataGrid1.TableStyles.Add(style)
DataGrid1.DataSource = dt
End Sub
End Class
Best regards,
Riquel
0 comments:
Post a Comment