一、基本操作
`DataGridView` 控件是 VB.NET 中用于显示和编辑数据的强大控件,常用于数据表格的展示和管理。### 1. **基本设置**
首先,你需要将 `DataGridView` 控件添加到你的窗体上。这可以通过设计器完成,也可以通过代码动态添加。
### 2. **绑定数据源**
`DataGridView` 控件可以绑定到多种数据源,例如 `DataTable`、`BindingList`、`List` 等。下面是将 `DataTable` 绑定到 `DataGridView` 的示例:
```vb
' 创建 DataTable
Dim dataTable As New DataTable()
dataTable.Columns.Add("ID", GetType(Integer))
dataTable.Columns.Add("Name", GetType(String))
dataTable.Columns.Add("Age", GetType(Integer))
' 添加数据行
dataTable.Rows.Add(1, "Alice", 30)
dataTable.Rows.Add(2, "Bob", 25)
' 绑定到 DataGridView
DataGridView1.DataSource = dataTable
```
### 3. **添加列**
你可以通过代码动态添加列到 `DataGridView`:
```vb
' 添加列
DataGridView1.Columns.Add("Column1", "Name")
DataGridView1.Columns.Add("Column2", "Age")
' 添加行
DataGridView1.Rows.Add("Alice", 30)
DataGridView1.Rows.Add("Bob", 25)
```
### 4. **自定义列**
你可以自定义列的类型和显示方式,例如使用 `DataGridViewCheckBoxColumn` 或 `DataGridViewComboBoxColumn`:
```vb
' 添加 CheckBox 列
Dim checkBoxColumn As New DataGridViewCheckBoxColumn()
checkBoxColumn.Name = "IsActive"
checkBoxColumn.HeaderText = "Active"
DataGridView1.Columns.Add(checkBoxColumn)
' 添加 ComboBox 列
Dim comboBoxColumn As New DataGridViewComboBoxColumn()
comboBoxColumn.Name = "Category"
comboBoxColumn.HeaderText = "Category"
comboBoxColumn.Items.Add("Category1")
comboBoxColumn.Items.Add("Category2")
DataGridView1.Columns.Add(comboBoxColumn)
```
### 5. **编辑和删除数据**
`DataGridView` 支持编辑和删除操作。默认情况下,用户可以直接在控件中进行编辑。你可以通过代码删除选中的行:
```vb
' 删除选中的行
For Each row As DataGridViewRow In DataGridView1.SelectedRows
DataGridView1.Rows.Remove(row)
Next
```
### 6. **处理事件**
你可以处理 `DataGridView` 的各种事件,例如 `CellValueChanged`、`RowEnter`、`CellClick` 等:
```vb
Private Sub DataGridView1_CellValueChanged(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellValueChanged
MessageBox.Show("Cell value changed!")
End Sub
Private Sub DataGridView1_RowEnter(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.RowEnter
MessageBox.Show("Row entered: " & e.RowIndex.ToString())
End Sub
```
### 7. **设置外观**
你可以通过代码或设计器设置 `DataGridView` 的外观,例如调整列宽、设置背景色等:
```vb
' 设置列宽
DataGridView1.Columns(0).Width = 100
' 设置行高
DataGridView1.RowTemplate.Height = 30
' 设置背景色
DataGridView1.BackgroundColor = Color.LightGray
```
### 8. **排序和筛选**
`DataGridView` 支持排序和筛选。你可以通过设置 `DataGridView` 的 `Sort` 方法来进行排序:
```vb
' 排序
DataGridView1.Sort(DataGridView1.Columns("Name"), System.ComponentModel.ListSortDirection.Ascending)
```
### 9. **分页**
`DataGridView` 默认不支持分页,但你可以结合 `BindingSource` 和 `DataTable` 来实现分页功能。分页通常涉及到数据的分段加载和显示,你需要手动管理页码和数据的切换
二、常用触发事件
### 1. **`CellClick`**
当用户单击 `DataGridView` 控件中的单元格时发生。
```vb
Private Sub DataGridView1_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellClick
MessageBox.Show("Cell clicked at row " & e.RowIndex & ", column " & e.ColumnIndex)
End Sub
```
### 2. **`CellValueChanged`**
当单元格的值发生更改时发生。需要启用 `DataGridView` 的 `CellValueChanged` 事件,并在 `CellValueChanged` 事件中处理。
```vb
Private Sub DataGridView1_CellValueChanged(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellValueChanged
MessageBox.Show("Cell value changed at row " & e.RowIndex & ", column " & e.ColumnIndex)
End Sub
```
### 3. **`RowEnter`**
当用户进入某一行时触发。
```vb
Private Sub DataGridView1_RowEnter(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.RowEnter
MessageBox.Show("Entered row " & e.RowIndex)
End Sub
```
### 4. **`RowLeave`**
当用户离开某一行时触发。
```vb
Private Sub DataGridView1_RowLeave(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.RowLeave
MessageBox.Show("Left row " & e.RowIndex)
End Sub
```
### 5. **`CellFormatting`**
在单元格绘制之前对其进行格式化。这对于修改单元格的显示样式很有用。
```vb
Private Sub DataGridView1_CellFormatting(sender As Object, e As DataGridViewCellFormattingEventArgs) Handles DataGridView1.CellFormatting
If e.ColumnIndex = 1 AndAlso e.Value IsNot Nothing Then
Dim value As Integer = Convert.ToInt32(e.Value)
If value > 50 Then
e.CellStyle.BackColor = Color.Yellow
End If
End If
End Sub
```
### 6. **`CellValidating`**
在单元格编辑完成并且失去焦点之前触发,用于验证输入。
```vb
Private Sub DataGridView1_CellValidating(sender As Object, e As DataGridViewCellValidatingEventArgs) Handles DataGridView1.CellValidating
If e.ColumnIndex = 0 AndAlso String.IsNullOrWhiteSpace(e.FormattedValue.ToString()) Then
DataGridView1.Rows(e.RowIndex).ErrorText = "This cell cannot be empty."
e.Cancel = True
End If
End Sub
```
### 7. **`CellEndEdit`**
当用户完成对单元格的编辑时触发。
```vb
Private Sub DataGridView1_CellEndEdit(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellEndEdit
MessageBox.Show("Finished editing cell at row " & e.RowIndex & ", column " & e.ColumnIndex)
End Sub
```
### 8. **`UserAddedRow`**
当用户添加新行时触发。这通常在启用用户添加行时发生。
```vb
Private Sub DataGridView1_UserAddedRow(sender As Object, e As DataGridViewRowEventArgs) Handles DataGridView1.UserAddedRow
MessageBox.Show("A new row was added.")
End Sub
```
### 9. **`RowsRemoved`**
当行被删除时触发。
```vb
Private Sub DataGridView1_RowsRemoved(sender As Object, e As DataGridViewRowsRemovedEventArgs) Handles DataGridView1.RowsRemoved
MessageBox.Show("Rows removed: " & e.RowCount)
End Sub
```
### 10. **`DataError`**
当 `DataGridView` 控件在数据绑定时遇到错误时触发。
```vb
Private Sub DataGridView1_DataError(sender As Object, e As DataGridViewDataErrorEventArgs) Handles DataGridView1.DataError
MessageBox.Show("Data error: " & e.Exception.Message)
End Sub
```
### 11. **`CellDoubleClick`**
当用户双击单元格时触发。
```vb
Private Sub DataGridView1_CellDoubleClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellDoubleClick
MessageBox.Show("Cell double-clicked at row " & e.RowIndex & ", column " & e.ColumnIndex)
End Sub
```
### 12. **`ColumnHeaderMouseClick`**
当用户点击列标题时触发。
```vb
Private Sub DataGridView1_ColumnHeaderMouseClick(sender As Object, e As DataGridViewCellMouseEventArgs) Handles DataGridView1.ColumnHeaderMouseClick
MessageBox.Show("Column header clicked at column " & e.ColumnIndex)
End Sub
```