Private Sub style()
Try
Dim strHeaders() As String = {"编号", "添加时间", "代码主题", "代码内容"} '中文列名
Dim iWidths() As Integer = {0, 70, 130, 0} '设为0隐藏显示
DataGrid1.DataSource = DataSet1.Tables("codes")
DataSet1.Tables("codes").DefaultView.AllowNew = False
DataSet1.Tables("codes").DefaultView.AllowEdit = False
Dim i As Integer
Dim ts As New DataGridTableStyle
ts.MappingName = DataSet1.Tables("codes").TableName
ts.AlternatingBackColor = System.Drawing.Color.PapayaWhip
ts.BackColor = System.Drawing.Color.WhiteSmoke
ts.SelectionBackColor = System.Drawing.Color.LightSteelBlue
ts.SelectionForeColor = Color.Black
ts.RowHeaderWidth = 10
ts.PreferredRowHeight = 20
For i = 0 To DataSet1.Tables("codes").Columns.Count - 1
Dim ac As New DataGridTextBoxColumn
AddHandler ac.TextBox.Enter, AddressOf TextBoxEnterHandler
ac.NullText = ""
'ac.ReadOnly = True
ac.HeaderText = strHeaders(i)
ac.MappingName = DataSet1.Tables("codes").Columns(i).ColumnName
ac.TextBox.Width = iWidths(i)
ts.PreferredColumnWidth = ac.TextBox.Width
ts.GridColumnStyles.Add(ac)
If i = 1 Or i = 2 Then
ac.Alignment = HorizontalAlignment.Center
End If
Next
DataGrid1.TableStyles.Clear()
DataGrid1.TableStyles.Add(ts)
Catch ex As Exception
If MessageBox.Show("格式化网格出错了,继续操作可能会出错! " & Chr(10) & " 仍然继续吗?", "格式化出错", MsgBoxStyle.YesNo, MessageBoxIcon.Question) = MsgBoxResult.No Then
End
End If
End Try
End Sub