问题描述
想在WINFORM的dataGridView里面用使用右键达到和左键一样的选中功能,新手求助!
解决方案
解决方案二:
新手求助!
解决方案三:
在CellMouseDown事件中写if(e.Button==MouseButtons.Right){if(e.RowIndex==-1)return;sGrid.Rows[e.RowIndex].Selected=true;}
解决方案四:
//写在CellMouseDown事件中privatevoidDataGridView_CellMouseDown(objectsender,DataGridViewCellMouseEventArgse){if(e.RowIndex>=0){if(e.Button==System.Windows.Forms.MouseButtons.Right){dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Selected=true;dataGridView1.ClearSelection();}}
解决方案五:
引用2楼bdmh的回复:
在CellMouseDown事件中写C#codeif(e.Button==MouseButtons.Right){if(e.RowIndex==-1)return;……
+1
解决方案六:
引用2楼bdmh的回复:
在CellMouseDown事件中写C#codeif(e.Button==MouseButtons.Right){if(e.RowIndex==-1)return;……
不行。显示错误码为缺少引用什么之类的?
解决方案七:
原来是我事件选错了,但是还有一个他表格打开默认选中的第一个格还存在的!
解决方案八:
引用3楼porschev的回复:
C#code//写在CellMouseDown事件中privatevoidDataGridView_CellMouseDown(objectsender,DataGridViewCellMouseEventArgse){if(e.RowIndex>=0){……
选中选中了。但是表格里面实际的值没有变。还是保留在原来左键选中的值里
解决方案九:
1.在你的项目里新建“XDataGridView.cs”的文件,把下面内容拷贝进去:usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Text;usingSystem.Windows.Forms;namespaceCommonTools.Control{[ToolboxItem(true),Category("ZteCommonTools"),Description("ZteCommonTools")]publicclassXDataGridView:DataGridView{privateXDataGridViewTypexType;[Browsable(true),Category("ZteCommonTools"),Description("DataGridView类型."),DefaultValue(XDataGridViewType.None)]publicXDataGridViewTypeXType{get{returnthis.xType;}set{this.xType=value;}}privateBooleanxRightMouseButtonSelect;[Browsable(true),Category("ZteCommonTools"),Description("允许鼠标右键选择DataGridViewRow."),DefaultValue(false)]publicBooleanXRightMouseButtonSelect{get{returnthis.xRightMouseButtonSelect;}set{this.xRightMouseButtonSelect=value;}}protectedoverridevoidOnCellMouseDown(DataGridViewCellMouseEventArgse){BooleansetAnchorCellAddress=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){caseKeys.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;caseKeys.Shift:setAnchorCellAddress=true;DataGridViewCellcell=this.CurrentCell;this.ClearSelection();if(cell.RowIndex<e.RowIndex){for(inti=cell.RowIndex;i<=e.RowIndex;i++){for(intj=cell.ColumnIndex;j<=e.ColumnIndex;j++){this.Rows[i].Cells[j].Selected=true;}}}else{for(inti=e.RowIndex;i<=cell.RowIndex;i++){for(intj=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”的文件,把下面内容拷贝进去:usingSystem;usingSystem.Collections.Generic;usingSystem.Text;namespaceCommonTools.Control{publicenumXDataGridViewType{///<summary>///无特殊功能限制的DataGridViewType.///</summary>None,}}
3.这时你项目的toolbox中会有一个XDataGridView的控件,是对DataGridView右键选中cell的支持,直接拖到UI上就可以了。4.注意,当你把XDataGridView控件拖到UI之后,你需要在属性栏里设置它的“XRightMouseButtonSelect”属性为true,这样就可以了。实际上这是我自己做的一个支持右键的DataGridView,lz可以把它整理成库,方便以后调用。
解决方案十:
补充一下,当你不设置“XRightMouseButtonSelect”属性时,他就是一个普通的DataGridView控件。
解决方案十一:
引用2楼bdmh的回复:
在CellMouseDown事件中写C#codeif(e.Button==MouseButtons.Right){if(e.RowIndex==-1)return;……
++
解决方案十二:
看看,不错!~
解决方案十三:
引用3楼porschev的回复:
C#code//写在CellMouseDown事件中privatevoidDataGridView_CellMouseDown(objectsender,DataGridViewCellMouseEventArgse){if(e.RowIndex>=0){……
good!