c# 如何用右键选中dataGridView的单元格

g63857850 2011-06-07 11:37:23
想在WINFORM的dataGridView里面用使用右键达到和左键一样的选中功能,新手求助!
...全文
723 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
ZhangNan20100811 2011-06-07
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 porschev 的回复:]
C# code

//写在CellMouseDown事件中
private void DataGridView_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
{
if (e.RowIndex >= 0)
{
……
[/Quote]

good!
荣之梦 2011-06-07
  • 打赏
  • 举报
回复
看看,不错!~
garfieldzf 2011-06-07
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 bdmh 的回复:]

在CellMouseDown事件中写
C# code

if (e.Button == MouseButtons.Right)
{
if (e.RowIndex == -1) return;
……
[/Quote]

++
Maa 2011-06-07
  • 打赏
  • 举报
回复
补充一下,当你不设置“XRightMouseButtonSelect”属性时,他就是一个普通的DataGridView控件。
Maa 2011-06-07
  • 打赏
  • 举报
回复
1.在你的项目里新建“XDataGridView.cs”的文件,把下面内容拷贝进去:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Windows.Forms;

namespace CommonTools.Control
{
[ToolboxItem(true), Category("ZteCommonTools"), Description("ZteCommonTools")]
public class XDataGridView : DataGridView
{
private XDataGridViewType xType;

[Browsable(true), Category("ZteCommonTools"), Description("DataGridView类型."), DefaultValue(XDataGridViewType.None)]
public XDataGridViewType XType
{
get { return this.xType; }
set { this.xType = value; }
}

private Boolean xRightMouseButtonSelect;

[Browsable(true), Category("ZteCommonTools"), Description("允许鼠标右键选择DataGridViewRow."), DefaultValue(false)]
public Boolean XRightMouseButtonSelect
{
get { return this.xRightMouseButtonSelect; }
set { this.xRightMouseButtonSelect = value; }
}

protected override void OnCellMouseDown(DataGridViewCellMouseEventArgs e)
{
Boolean setAnchorCellAddress = false;

base.OnCellMouseDown(e);

if (!e.Button.Equals(MouseButtons.Right)) return; // 只处理鼠标右键.
if (e.RowIndex.Equals(-1) || e.ColumnIndex.Equals(-1)) return; // 不处理RowHeader或ColumnHeader上的响应事件.
if (!this.xRightMouseButtonSelect) return; // 只处理开启"右键选择"功能的响应事件.

switch (ModifierKeys)
{
case Keys.Control:
this.Rows[e.RowIndex].Cells[e.ColumnIndex].Selected = !this.Rows[e.RowIndex].Cells[e.ColumnIndex].Selected;
this.SetCurrentCellAddressCore(e.ColumnIndex, e.RowIndex, false, true, true);
break;
case Keys.Shift:
setAnchorCellAddress = true;
DataGridViewCell cell = this.CurrentCell;
this.ClearSelection();
if (cell.RowIndex < e.RowIndex)
{
for (int i = cell.RowIndex; i <= e.RowIndex; i++)
{
for (int j = cell.ColumnIndex; j <= e.ColumnIndex; j++)
{
this.Rows[i].Cells[j].Selected = true;
}
}
}
else
{
for (int i = e.RowIndex; i <= cell.RowIndex; i++)
{
for (int j = e.ColumnIndex; j <= cell.ColumnIndex; j++)
{
this.Rows[i].Cells[j].Selected = true;
}
}
}
break;
default:
this.ClearSelection();
this.Rows[e.RowIndex].Cells[e.ColumnIndex].Selected = !this.Rows[e.RowIndex].Cells[e.ColumnIndex].Selected;
this.SetCurrentCellAddressCore(e.ColumnIndex, e.RowIndex, false, true, true);
break;
}
}
}
}



2.在你的项目里新建“XDataGridViewType.cs”的文件,把下面内容拷贝进去:

using System;
using System.Collections.Generic;
using System.Text;

namespace CommonTools.Control
{
public enum XDataGridViewType
{
/// <summary>
/// 无特殊功能限制的DataGridViewType.
/// </summary>
None,
}
}



3.这时你项目的toolbox中会有一个XDataGridView的控件,是对DataGridView右键选中cell的支持,直接拖到UI上就可以了。
4.注意,当你把XDataGridView控件拖到UI之后,你需要在属性栏里设置它的“XRightMouseButtonSelect”属性为true,这样就可以了。

实际上这是我自己做的一个支持右键的DataGridView,lz可以把它整理成库,方便以后调用。
g63857850 2011-06-07
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 porschev 的回复:]
C# code

//写在CellMouseDown事件中
private void DataGridView_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
{
if (e.RowIndex >= 0)
{
……
[/Quote]
选中选中了。但是表格里面实际的值没有变。还是保留在原来左键选中的值里
g63857850 2011-06-07
  • 打赏
  • 举报
回复
原来是我事件选错了,但是还有一个他表格打开默认选中的第一个格还存在的!
g63857850 2011-06-07
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 bdmh 的回复:]
在CellMouseDown事件中写

C# code

if (e.Button == MouseButtons.Right)
{
if (e.RowIndex == -1) return;
……
[/Quote]

不行。显示错误码为缺少引用什么之类的?
tigercao101 2011-06-07
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 bdmh 的回复:]
在CellMouseDown事件中写

C# code

if (e.Button == MouseButtons.Right)
{
if (e.RowIndex == -1) return;
……
[/Quote]
+1
porschev 2011-06-07
  • 打赏
  • 举报
回复


//写在CellMouseDown事件中
private void DataGridView_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
{
if (e.RowIndex >= 0)
{
if (e.Button == System.Windows.Forms.MouseButtons.Right)
{
dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Selected = true;
dataGridView1.ClearSelection();
}
}

bdmh 2011-06-07
  • 打赏
  • 举报
回复
在CellMouseDown事件中写

if (e.Button == MouseButtons.Right)
{
if (e.RowIndex == -1) return;
sGrid.Rows[e.RowIndex].Selected = true;
}
g63857850 2011-06-07
  • 打赏
  • 举报
回复
新手求助!

110,536

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧