欢迎来到天天文库
浏览记录
ID:40489642
大小:25.75 KB
页数:6页
时间:2019-08-03
《C# DataGridView添加右键菜单等技巧》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、1).右键点击行时选中行,并弹出操作菜单1).添加一个快捷菜单contextMenuStrip1;2).给dataGridView1的CellMouseDown事件添加处理程序:复制内容到剪贴板 程序代码privatevoidDataGridView1_CellMouseDown(objectsender,DataGridViewCellMouseEventArgse){ if(e.Button==MouseButtons.Right) { if(e.RowIndex>=0) {
2、 //若行已是选中状态就不再进行设置 if(dataGridView1.Rows[e.RowIndex].Selected==false) { dataGridView1.ClearSelection(); dataGridView1.Rows[e.RowIndex].Selected=true; } //只选中一行时设置活动单元格 if(dataGridView1.Sel
3、ectedRows.Count==1) { dataGridView1.CurrentCell=dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex]; } //弹出操作菜单 contextMenuStrip1.Show(MousePosition.X,MousePosition.Y); } }}效果图:2).复制选中单元格的内容到剪贴板复制内容到剪贴板 程
4、序代码Clipboard.SetDataObject(dataGridView1.GetClipboardContent());3).只显示自定义列复制内容到剪贴板 程序代码dataGridView1.AutoGenerateColumns=false;//必须在代码中设置4).显示图片通常,我们将图片路径保存在数据库中,但在dataGridView1中要显示图片,可以进行如下操作:①.添加一个DataGridViewTextBoxColumn类型的列,Name=Path,DataPropertyName=Pic,Visible=False
5、;②.添加一个DataGridViewImageColumn类型的列,Name=Pic;③.dataGridView1控件DataBindingComplete事件处理程序如下:复制内容到剪贴板 程序代码privatevoidDataGridView1_DataBindingComplete(objectsender,DataGridViewBindingCompleteEventArgse){ Imageimage1=null; Imageimage2=null; stringpath=string.Empty;
6、for(inti=0;i7、ells["Pic"].Value=image2; //((DataGridViewImageCell)dataGridView1.Rows[i].Cells["Pic"]).Value=image2; } }}5).当网格未填充满控件时,画线来填充空白区域复制内容到剪贴板 程序代码//////绘制网格填充空白区域/////////publicvoidCellP8、ainting(objectsender,DataGridViewCellPaintingEventArgse){ DataGridViewmyDataGridView=(
7、ells["Pic"].Value=image2; //((DataGridViewImageCell)dataGridView1.Rows[i].Cells["Pic"]).Value=image2; } }}5).当网格未填充满控件时,画线来填充空白区域复制内容到剪贴板 程序代码//////绘制网格填充空白区域/////////publicvoidCellP
8、ainting(objectsender,DataGridViewCellPaintingEventArgse){ DataGridViewmyDataGridView=(
此文档下载收益归作者所有