8.取消默认选中行
整行选择时:datagridview.SelectedRows(0).Selected = False
9.获取选中行某列的值
Private Sub dgvCheck_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles dgvCheck.Click
Dim ss As String
ss = dgvCheck.SelectedCells.Item(0).Value
End Sub
10.DataGridView的行背景色(奇数行与偶数行的背景色差异)
'全ての列の背景色を水色にする
DataGridView1.RowsDefaultCellStyle.BackColor = Color.Aqua
'奇数行を黄色にする
DataGridView1.AlternatingRowsDefaultCellStyle.BackColor = Color.Yellow
11.变更列标题名字
'DataGridView1のはじめの列のテキストを変更する
DataGridView1.Columns(0).HeaderText = "はじめの列"
12.鼠标指向的单元格颜色变更
'DataGridView1のCellMouseEnterイベントハンドラ
Private Sub DataGridView1_CellMouseEnter(ByVal sender As Object, _
ByVal e As DataGridViewCellEventArgs) _ Handles DataGridView1.CellMouseEnter
'ヘッダー以外のセル
If e.ColumnIndex >= 0 And e.RowIndex >= 0 Then
Dim dgv As DataGridView = CType(sender, DataGridView)
'セルスタイルを変更する
dgv(e.ColumnIndex, e.RowIndex).Style.BackColor = Color.Red
dgv(e.ColumnIndex, e.RowIndex).Style.SelectionBackColor = Color.Red
End If
End Sub
'DataGridView1のCellMouseLeaveイベントハンドラ
Private Sub DataGridView1_CellMouseLeave(ByVal sender As Object, _
ByVal e As DataGridViewCellEventArgs) _
Handles DataGridView1.CellMouseLeave
'ヘッダー以外のセル
If e.ColumnIndex >= 0 And e.RowIndex >= 0 Then
Dim dgv As DataGridView = CType(sender, DataGridView)
'セルスタイルを元に戻す
'セルスタイルを削除するなら、nullを設定してもよい
dgv(e.ColumnIndex, e.RowIndex).Style.BackColor = Color.Empty
dgv(e.ColumnIndex, e.RowIndex).Style.SelectionBackColor = Color.Empty
End If
End Sub