Datagridview使用技巧(9)Datagridview的右键菜单
DataGridView,DataGridViewColumn,DataGridViewRow,DataGridViewCell有ContextMenuStrip属性。可以通过设置ContextMenuStrip对象来控制DataGridView的右键菜单的显示。 DataGridViewColumn的ContextMenuStrip属性设定除了列头以外的单元格的右键菜单。 DataGridViewRow的ContextMenuStrip属性设定除了行头以外的单元格的右键菜单。 DataGridViewCell的ContextMenuStrip属性设定指定单元格的右键菜单。 对于单元格上的右键菜单的设定,优先顺序是:Cell>Row>Column>DataGridView 利用CellContextMenuStripNeeded、RowContextMenuStripNeeded事件可以设定单元格的右键菜单,尤其是需要右键菜单根据单元格值的变化而变化的时候。比起使用循环遍历,使用该事件来设定右键菜单的效率更高。 说明:CellContextMenuStripNeeded事件处理方法的参数中,e.RowIndex=-1表示列头,e.ColumnIndex=-1表示行头。RowContextMenuStripNeeded则不存在e.ColumnIndex=-1的情况。 示例一: //设置DataGridView的右键菜单 this.dgv_Users.ContextMenuStrip = cmsDgv; //设置列的右键菜单 this.dgv_Users.Columns[1].ContextMenuStrip = cmsColumn; //设置列头的右键菜单 this.dgv_Users.Columns[1].HeaderCell.ContextMenuStrip = cmsHeaderCell; //设置行的右键菜单 this.dgv_Users.Rows[2].ContextMenuStrip = cmsRow; //设置单元格的右键菜单 this.dgv_Users[1,2].ContextMenuStrip = cmsCell; 示例二: private void dgv_Users_CellContextMenuStripNeeded(object sender,DataGridViewCellContextMenuStripNeededEventArgs e) { DataGridView dgv = sender as DataGridView; if (e.RowIndex < 0) { //设置列头右键 e.ContextMenuStrip = cmsHeaderCell; } else if (e.ColumnIndex < 0) { //设置行头右键菜单 e.ContextMenuStrip = cmsRow; } else if (dgv[e.ColumnIndex,e.RowIndex].Value.ToString().Equals("男")) { e.ContextMenuStrip = cmsCell; } else { e.ContextMenuStrip = cmsDgv; } } 以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程小技巧。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- ruby-on-rails – rails form_tag url path
- Dagger - 快速依赖注入器(for android and java)
- Cocos2d-x3.3beta0创建动画的3种方式
- React Native 0.20 发布 (翻译 changelog)
- Vuex中mutations与actions的区别详解
- React-Native:在ListView中显示JSON数据
- c# – MySql的异常:变量’character_set_client
- / etc / passwd中的行的正则表达式验证
- PostgreSQL与MySQL对比
- pugixml 1.9 manual解读(部分)