重绘datagrid,包括强迫显示某行
#Region "用来对DataGrid的显示样式进行重绘"
Private Sub FormatGridWithTableStyles()
Dim ts As New DataGridTableStyle '就是它决定了datagrid是什么样的
Dim aColumnTextColumn As DataGridTextBoxColumn '要重写的东东
ts.MappingName = datagridTable.TableName
Dim numCols As Integer
numCols = datagridTable.Columns.Count
Dim i As Integer = 0
Do While (i < numCols) '重绘所有的列
aColumnTextColumn = New DataGridTextBoxColumn
'夺过对textbox内落下光标的控制权
AddHandler aColumnTextColumn.TextBox.Enter, New EventHandler(AddressOf TextBoxEnterHandler)
'要更改列头名,请改下句的HeaderText值
aColumnTextColumn.HeaderText = datagridTable.Columns(i).ColumnName
'更改列的实际内容
aColumnTextColumn.MappingName = datagridTable.Columns(i).ColumnName
'自动改变行宽不对
aColumnTextColumn.TextBox.AutoSize = True
'MsgBox(aColumnTextColumn.HeaderText.ToString)
'Debug.Write(aColumnTextColumn.TextBox.Width & "aa")
'自动改变行宽不对
aColumnTextColumn.TextBox.AutoSize = True
ts.PreferredColumnWidth = aColumnTextColumn.TextBox.Width()
ts.GridColumnStyles.Add(aColumnTextColumn) '增加一种自定义的column风格
i = (i + 1)
Loop
DataGrid1.TableStyles.Clear()
DataGrid1.TableStyles.Add(ts) '增加一种自定义的表风格
End Sub
#End Region
#Region "当点击时强迫datagrid选中某行,而不是某行某列"
Private Sub TextBoxEnterHandler(ByVal sender As Object, ByVal e As System.EventArgs)
Dim inner As Control = sender
inner.Visible = False
DataGrid1.Select(DataGrid1.CurrentRowIndex)
End Sub
#End Region