谢谢
Sub datagrid_fill()
'填充显示
DataGrid1.TableStyles.Clear()
DataGrid1.Visible = True
DataGrid1.Dock = DockStyle.Fill
mydataset.Tables.Clear()
oledbconn = New OleDb.OleDbConnection(strConnect)
oledbadapter = New OleDbDataAdapter(sql, oledbconn)
oledbadapter.Fill(mydataset, "表名")
oledbadapter.Dispose()
oledbconn.Close()
mytable = mydataset.Tables("表名")
DataGrid1.DataSource = mytable
AddHandler mytable.ColumnChanged, New DataColumnChangeEventHandler(AddressOf columnchanged)
AddHandler mytable.RowDeleted, New DataRowChangeEventHandler(AddressOf rowdeleted)
Dim ts As New DataGridTableStyle
Dim aColumnTextColumn As DataGridTextBoxColumn
ts.MappingName = mytable.TableName
Dim numCols As Integer
numCols = mytable.Columns.Count
Dim i As Integer = 0
Do While (i < numCols) '重绘所有的列
aColumnTextColumn = New DataGridTextBoxColumn
AddHandler aColumnTextColumn.TextBox.KeyDown, New KeyEventHandler(AddressOf Column_Keydown)
'让列中的单元格来响应KeyPress事件
aColumnTextColumn.HeaderText = mytable.Columns(i).ColumnName
aColumnTextColumn.MappingName = mytable.Columns(i).ColumnName
aColumnTextColumn.Width = 20 '设置列宽
ts.GridColumnStyles.Add(aColumnTextColumn) '增加一种自定义的column风格
i = (i + 1)
Loop
DataGrid1.TableStyles.Clear()
DataGrid1.TableStyles.Add(ts) '增加一种自定义的表风格
'事件处理程序,同样适用于其它Textbox用于数值框处理的情况。
'MessageBox.Show("增加了样式风格")
End Sub