先将datagrid内嵌的textbox设为自动适应文字长短的形式,再将column 的宽度设为与textbox一致
Dim dt As DataTable
Dim ts As New DataGridTableStyle()
Dim dgcs As DataGridColumnStyle
Dim dgtbc As DataGridTextBoxColumn
dt = DataSet11.Tables("table")
DataGrid1.DataSource = dt
ts.MappingName = dt.TableName
Dim numCols As Integer
numCols = DataSet11.Tables("table").Columns.Count
Dim aColumnTextColumn As DataGridTextBoxColumn
Dim i As Integer
i = 0
Do While (i < numCols)
aColumnTextColumn = New DataGridTextBoxColumn()
aColumnTextColumn.TextBox.AutoSize = True
ts.PreferredColumnWidth = aColumnTextColumn.TextBox.Width
aColumnTextColumn.HeaderText = DataSet11.Tables("table").Columns(i).ColumnName
aColumnTextColumn.MappingName = DataSet11.Tables("table").Columns(i).ColumnName
ts.GridColumnStyles.Add(aColumnTextColumn)
i = i + 1
Loop
DataGrid1.TableStyles.Add(ts)