云题海 - 专业文章范例文档资料分享平台

当前位置:首页 > 超全经典DataGridView+编程36计

超全经典DataGridView+编程36计

  • 62 次阅读
  • 3 次下载
  • 2025/12/12 2:49:38

{

isHeader = false; continue; }

// 按 Tab 分割数据

string[] vals = line.Split(' '); // 判断列数是否统一

if (vals.Length - 1 != DataGridView1.ColumnCount)

throw new ApplicationException(\粘贴的列数不正确。\ DataGridViewRow row = DataGridView1.Rows[insertRowIndex]; // 行头设定

row.HeaderCell.Value = vals[0]; // 单元格内容设定

for (int i = 0; i < row.Cells.Count; i++) {

row.Cells[i].Value = vals[i + 1]; }

// DataGridView的行索引+1 insertRowIndex++; }

? DataGridView 单元格的ToolTip的设置

GO TO TOP

DataGridView.ShowCellToolTips = True 的情况下, 单元格的 ToolTip 可以表示出来。对于单元格窄小,无法完全显示的单元格, ToolTip 可以显示必要的信息。

1) 设定单元格的ToolTip内容 [VB.NET]

' 设定单元格的ToolTip内容

DataGridView1(0, 0).ToolTipText = \该单元格的内容不能修改\

' 设定列头的单元格的ToolTip内容

DataGridView1.Columns(0).ToolTipText = \该列只能输入数字\

' 设定行头的单元格的ToolTip内容

DataGridView1.Rows(0).HeaderCell.ToolTipText = \该行单元格内容不能修改\ [C#]

// 设定单元格的ToolTip内容

DataGridView1[0, 0].ToolTipText = \该单元格的内容不能修改\

// 设定列头的单元格的ToolTip内容

DataGridView1.Columns[0].ToolTipText = \该列只能输入数字\

// 设定行头的单元格的ToolTip内容

DataGridView1.Rows[0].HeaderCell.ToolTipText = \该行单元格内容不能修改\

2) CellToolTipTextNeeded 事件

在批量的单元格的 ToolTip 设定的时候,一个一个指定那么设定的效率比较低, 这时候可以利用 CellToolTipTextNeeded 事件。当单元格的 ToolTipText 变化的时候也会引发该事件。但是,当DataGridView的DataSource被指定且VirualMode=True的时候,该事件不会被引发。

[VB.NET]

' CellToolTipTextNeeded事件处理方法

Private Sub DataGridView1_CellToolTipTextNeeded(ByVal sender As Object, _

ByVal e As DataGridViewCellToolTipTextNeededEventArgs) _ Handles DataGridView1.CellToolTipTextNeeded

e.ToolTipText = e.ColumnIndex.ToString() + \ring() End Sub [C#]

// CellToolTipTextNeeded事件处理方法

private void DataGridView1_CellToolTipTextNeeded(object sender, DataGridViewCellToolTipTextNeededEventArgs e) {

e.ToolTipText = e.ColumnIndex.ToString() + \ring(); }

? DataGridView 的右键菜单(ContextMenuStrip)

GO TO TOP

DataGridView, DataGridViewColumn, DataGridViewRow, DataGridViewCell 有 ContextMenuStrip 属性。可以通过设定 ContextMenuStrip 对象来控制 DataGridView 的右键菜单的显示。 DataGridViewColumn 的 ContextMenuStrip 属性设定了 除了列头以外的单元格的右键菜单。 DataGridViewRow 的 ContextMenuStrip 属性设定了除了行头以外的单元格的右键菜单。

DataGridViewCell 的 ContextMenuStrip 属性设定了指定单元格的右键菜单。

[VB.NET]

' DataGridView 的 ContextMenuStrip 设定

DataGridView1.ContextMenuStrip = Me.ContextMenuStrip1

' 列的 ContextMenuStrip 设定

DataGridView1.Columns(0).ContextMenuStrip = Me.ContextMenuStrip2 ' 列头的 ContextMenuStrip 设定

DataGridView1.Columns(0).HeaderCell.ContextMenuStrip = Me.ContextMenuStrip2

' 行的 ContextMenuStrip 设定

DataGridView1.Rows(0).ContextMenuStrip = Me.ContextMenuStrip3

' 单元格的 ContextMenuStrip 设定

DataGridView1(0, 0).ContextMenuStrip = Me.ContextMenuStrip4 [C#]

// DataGridView 的 ContextMenuStrip 设定

DataGridView1.ContextMenuStrip = this.ContextMenuStrip1;

// 列的 ContextMenuStrip 设定

DataGridView1.Columns[0].ContextMenuStrip = this.ContextMenuStrip2; // 列头的 ContextMenuStrip 设定

DataGridView1.Columns[0].HeaderCell.ContextMenuStrip = this.ContextMenuStrip2;

// 行的 ContextMenuStrip 设定

DataGridView1.Rows[0].ContextMenuStrip = this.ContextMenuStrip3;

// 单元格的 ContextMenuStrip 设定

DataGridView1[0, 0].ContextMenuStrip = this.ContextMenuStrip4; 对于单元格上的右键菜单的设定,优先顺序是: Cell > Row > Column > DataGridView

? CellContextMenuStripNeeded、RowContextMenuStripNeeded 事件 利用 CellContextMenuStripNeeded 事件可以设定单元格的右键菜单,尤其但需要右键菜单根据单元格值的变化而变化的时候。比起使用循环遍历,使用该事件来设定右键菜单的效率更高。但是,在DataGridView使用了DataSource绑定而且是VirtualMode的时候,该事件将不被引发。

[VB.NET]

' CellContextMenuStripNeeded事件处理方法

Private Sub DataGridView1_CellContextMenuStripNeeded( _ ByVal sender As Object, _

ByVal e As DataGridViewCellContextMenuStripNeededEventArgs) _ Handles DataGridView1.CellContextMenuStripNeeded Dim dgv As DataGridView = CType(sender, DataGridView) If e.RowIndex < 0 Then

' 列头的ContextMenuStrip设定

e.ContextMenuStrip = Me.ContextMenuStrip1 ElseIf e.ColumnIndex < 0 Then

' 行头的ContextMenuStrip设定

e.ContextMenuStrip = Me.ContextMenuStrip2

ElseIf TypeOf (dgv(e.ColumnIndex, e.RowIndex).Value) Is Integer Then

' 如果单元格值是整数时

e.ContextMenuStrip = Me.ContextMenuStrip3 End If End Sub [C#]

// CellContextMenuStripNeeded事件处理方法

private void DataGridView1_CellContextMenuStripNeeded(object sender, DataGridViewCellContextMenuStripNeededEventArgs e) {

DataGridView dgv = (DataGridView)sender; if (e.RowIndex < 0) {

// 列头的ContextMenuStrip设定

e.ContextMenuStrip = this.ContextMenuStrip1; }

else if (e.ColumnIndex < 0) {

// 行头的ContextMenuStrip设定

e.ContextMenuStrip = this.ContextMenuStrip2; }

else if (dgv[e.ColumnIndex, e.RowIndex].Value is int) {

// 如果单元格值是整数时

e.ContextMenuStrip = this.ContextMenuStrip3; } }

同样,可以通过 RowContextMenuStripNeeded 事件来设定行的右键菜单。 [VB.NET]

' RowContextMenuStripNeeded事件处理方法

Private Sub DataGridView1_RowContextMenuStripNeeded( _ ByVal sender As Object, _

ByVal e As DataGridViewRowContextMenuStripNeededEventArgs) _

搜索更多关于: 超全经典DataGridView+编程36计 的文档
  • 收藏
  • 违规举报
  • 版权认领
下载文档10.00 元 加入VIP免费下载
推荐下载
本文作者:...

共分享92篇相关文档

文档简介:

{ isHeader = false; continue; } // 按 Tab 分割数据 string[] vals = line.Split(' '); // 判断列数是否统一 if (vals.Length - 1 != DataGridView1.ColumnCount) throw new ApplicationException(\粘贴的列数不正确。\ DataGridViewRow row = DataGridView1.Rows[insertRowIndex]; // 行头设定 row.HeaderCell.Value = vals[0]; // 单元格内容设

× 游客快捷下载通道(下载后可以自由复制和排版)
单篇付费下载
限时特价:10 元/份 原价:20元
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信:fanwen365 QQ:370150219
Copyright © 云题海 All Rights Reserved. 苏ICP备16052595号-3 网站地图 客服QQ:370150219 邮箱:370150219@qq.com