datagridview与contextmenustrip完美结合应用

datagridview与contextmenustrip完美结合应用

上图是最佳datagridview与contextmenustrip合用效果,方法是:

datagridview与contextmenustrip完美结合应用

而当两个以上的datagridview共同使用一个ContextMenuStrip时,【打开】【关闭】处理事件识别SourceControl的方法:


  1. private void 打开ToolStripMenuItem_Click(object sender, EventArgs e)
  2. {
  3.   ContextMenuStrip cmStrip = (ContextMenuStrip)((ToolStripMenuItem)sender).Owner;
  4.   string ctrlSrcName = cmStrip.SourceControl.Name;
  5.   if (ctrlSrcName.Equals("dataGridView3"))
  6.   {}
  7.   else if (ctrlSrcName.Equals("dataGridView4"))
  8.   {}
  9. }


附动态右键生成contextMenuStrip的方法:


  1. private void dataGridView4_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
  2. {
  3.     if (e.Button == System.Windows.Forms.MouseButtons.Right)
  4.     {
  5.         if (e.RowIndex >= 0)
  6.         {
  7.             dataGridView4.ClearSelection();
  8.             dataGridView4.Rows[e.RowIndex].Selected = true;
  9.             dataGridView4.CurrentCell = dataGridView4.Rows[e.RowIndex].Cells[e.ColumnIndex];
  10.             this.contextMenuStrip1.Tag = "dataGridView4";
  11.             this.contextMenuStrip1.Show(MousePosition.X, MousePosition.Y);
  12.         }
  13.     }
  14. }


上一篇:关于session.auto_start报警的问题


下一篇:MySQL安装过程启动mysqld_safe中提示的pid ended错误导致无法启动问题处理