回复 5楼junlingzhu2002的帖子
你可以写一个自定的ContextMenuStrip,比如下面:
- class MyRowHeaderContextMenuStrip : ContextMenuStrip
- {
- private GcMultiRow _gcMultiROw;
- private int _rowIndexCache;
- public MyRowHeaderContextMenuStrip(GcMultiRow gcMultiRow)
- {
- _gcMultiROw = gcMultiRow;
- _gcMultiROw.CellMouseUp += _gcMultiROw_CellMouseUp;
- this.Items.Add("AddRow", null, new EventHandler(OnItemClick));
- this.Items.Add("RemoveRow", null, new EventHandler(OnItemClick));
- }
- void _gcMultiROw_CellMouseUp(object sender, CellMouseEventArgs e)
- {
- if (e.Scope == CellScope.Row)
- {
- _rowIndexCache = e.RowIndex;
- }
- }
- private void OnItemClick(object sender, EventArgs e)
- {
- ToolStripItem item = sender as ToolStripItem;
- if (item != null)
- {
- if (item.Text == "AddRow")
- {
- _gcMultiROw.Rows.Insert(_rowIndexCache);
- }
- else if (item.Text == "RemoveRow")
- {
- _gcMultiROw.Rows.RemoveAt(_rowIndexCache);
- }
- }
- }
- }
复制代码
然后在Form_Loaded事件处理函数当中构造一个自定义的MyRowHeaderContextMenuStrip,设置给对应的RowHeaderCell.ContextMenuStrip属性:
- private void Form1_Load(object sender, EventArgs e)
- {
- //假定Cell0是RowHeaderCell
- this.gcMultiRow1.Template.Row.Cells[0].ContextMenuStrip = this.myRowHeaderContextMenuStrip1;
- //重新加载模板
- this.gcMultiRow1.Template = this.gcMultiRow1.Template;
- }
复制代码 |