1、DataGridView 控件用法合集DataGridView 控件用法 211. DataGridView 行复数选择复数行选择不可DataGridView1.MultiSelect = False单元格选择的时候默认为选择整行DataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect12. DataGridView 选择的行、列、单元格取得Console.WriteLine(“選択“)For Each c As DataGridViewCell In DataGridView1.SelectedCellsCon
2、sole.WriteLine(“0, 1“, c.ColumnIndex, c.RowIndex)Next cConsole.WriteLine(“選択行“)For Each r As DataGridViewRow In DataGridView1.SelectedRowsConsole.WriteLine(r.Index)Next rConsole.WriteLine(“選択列“)For Each c As DataGridViewColumn In DataGridView1.SelectedColumnsConsole.WriteLine(c.Index)Next c指定行、列、单元格
3、取得DataGridView1(0, 0).Selected = TrueDataGridView1.Rows(1).Selected = TrueDataGridView1.Columns(2).Selected = True13. DataGridView 指定单元格是否表示If Not DataGridView1(0, 0).Displayed AndAlso _DataGridView1(0, 0).Visible ThenDataGridView1.CurrentCell = DataGridView1(0, 0)End If14. DataGridView 表头部单元格取得Data
4、GridView1.Columns(0).HeaderCell.Value = “列“DataGridView1.Rows(0).HeaderCell.Value = “行“DataGridView1.TopLeftHeaderCell.Value = “左上“15. DataGridView 表头部单元格文字列设定更改列 Header 表示文字列DataGridView1.Columns(0).HeaderText = “列“更改行 Header 表示文字列DataGridView 控件用法合集Dim i As IntegerFor i = 0 To DataGridView1.Rows.C
5、ount - 1DataGridView1.Rows(i).HeaderCell.Value = i.ToString()Next iDataGridView1.AutoResizeRowHeadersWidth(DataGridViewRowHeadersWidthSizeMode.AutoSizeToAllHeaders)最左上 Header 单元格文字列DataGridView1.TopLeftHeaderCell.Value = “/“16. DataGridView 选择的部分拷贝至剪贴板拷贝模式设定DataGridView1.ClipboardCopyMode = DataGrid
6、ViewClipboardCopyMode.EnableWithoutHeaderText选中部分拷贝Clipboard.SetDataObject(DataGridView1.GetClipboardContent()17.DataGridView 粘贴If DataGridView1.CurrentCell Is Nothing ThenReturnEnd IfDim insertRowIndex As Integer = DataGridView1.CurrentCell.RowIndexDim pasteText As String = Clipboard.GetText()If St
7、ring.IsNullOrEmpty(pasteText) ThenReturnEnd IfpasteText = pasteText.Replace(vbCrLf, vbLf)pasteText = pasteText.Replace(vbCr, vbLf)pasteText.TrimEnd(New Char() vbLf)Dim lines As String() = pasteText.Split(vbLf)Dim isHeader As Boolean = TrueFor Each line As String In linesIf isHeader ThenisHeader = Fa
8、lseElseDim vals As String() = line.Split(ControlChars.Tab)If vals.Length - 1 DataGridView1.ColumnCount ThenThrow New ApplicationException(“列数違。“)End IfDim row As DataGridViewRow = DataGridView1.Rows(insertRowIndex)row.HeaderCell.Value = vals(0)Dim i As IntegerFor i = 0 To row.Cells.Count - 1DataGrid
9、View 控件用法合集row.Cells(i).Value = vals(i + 1)Next iinsertRowIndex += 1End IfNext line18. DataGridView 单元格上 ToolTip 表示设定(鼠标移动到相应单元格上时,弹出说明信息 )DataGridView1(0, 0).ToolTipText = “変更“DataGridView1.Columns(0).ToolTipText = “列数字入力“DataGridView1.Rows(0).HeaderCell.ToolTipText = “行変更“CellToolTipTextNeeded 事件,
10、在多个单元格使用相同的 ToolTips 的时候,可以用该事件,下例为显示当前单元格的行号和列号Private Sub DataGridView1_CellToolTipTextNeeded(ByVal sender As Object, _ByVal e As DataGridViewCellToolTipTextNeededEventArgs) _Handles DataGridView1.CellToolTipTextNeedede.ToolTipText = e.ColumnIndex.ToString() + “, “ + e.RowIndex.ToString()End Sub19
11、. DataGridView 中的 ContextMenuStrip 属性DataGridView1.ContextMenuStrip = Me.ContextMenuStrip1DataGridView1.Columns(0).ContextMenuStrip = Me.ContextMenuStrip2DataGridView1.Columns(0).HeaderCell.ContextMenuStrip = Me.ContextMenuStrip2DataGridView1.Rows(0).ContextMenuStrip = Me.ContextMenuStrip3DataGridVi
12、ew1(1, 0).ContextMenuStrip = Me.ContextMenuStrip4也可以用 CellContextMenuStripNeeded、RowContextMenuStripNeeded 属性进行定义Private Sub DataGridView1_CellContextMenuStripNeeded( _ByVal sender As Object, _ByVal e As DataGridViewCellContextMenuStripNeededEventArgs) _Handles DataGridView1.CellContextMenuStripNeed
13、edDim dgv As DataGridView = CType(sender, DataGridView)If e.RowIndex 0 Thene.ContextMenuStrip = Me.ContextMenuStrip1ElseIf e.ColumnIndex 0 Thene.ContextMenuStrip = Me.ContextMenuStrip2ElseIf TypeOf (dgv(e.ColumnIndex, e.RowIndex).Value) Is Integer Thene.ContextMenuStrip = Me.ContextMenuStrip3End IfEnd Sub20.指定 DataGridView 的滚动框位置DataGridView1.FirstDisplayedScrollingRowIndex = 0DataGridView1.FirstDisplayedScrollingColumnIndex = 0