zoukankan      html  css  js  c++  java
  • 1、DataGridView中实现右键单击后选中所在行及弹出上下文菜单

    1、DataGridView中实现右键单击后选中所在行及弹出上下文菜单
      首先对dataGridView1添加RowContextMenuStripNeeded事件,然后新建一个DataGridViewRow,通过e.RowIndex捕获鼠标右键单击的行信息,清除所有选中的行,将鼠标右键捕获到的行设为选中。
      在这里通过dataGridViewRow1.Cells["fullNameDataGridViewTextBoxColumn"].Value将所选单元格的内容传给变量,附代码如下。
      private void dataGridView1_RowContextMenuStripNeeded(object sender, DataGridViewRowContextMenuStripNeededEventArgs e)
      {
      DataGridViewRow dataGridViewRow1 = dataGridView1.Rows[e.RowIndex];
      dataGridView1.ClearSelection();
      dataGridViewRow1.Selected = true;
      toolStripMenuItem1.Enabled = true;
      fullName = dataGridViewRow1.Cells["fullNameDataGridViewTextBoxColumn"].Value as string;
      // if ((dataGridViewRow1.Cells["Title"].Value.ToString() ==
      //"Sales Manager") ||
      //(dataGridViewRow1.Cells["Title"].Value.ToString() ==
      //"Vice President, Sales"))
      // {
      // e.ContextMenuStrip = managerMenuStrip;
      // }
      // else
      // {
      // e.ContextMenuStrip = employeeMenuStrip;
      // }
      // contextMenuRowIndex = e.RowIndex;
      }
      --------------------------------------------------------------------------------------------
      2、DataGridView中通过事件捕获所选行的ID号及单元格内容
      主要是通过dataGridView1.Rows[dataGridView1.CurrentCellAddress.Y].Cells["列名"].Value捕获鼠标单击的行及指定列的值。
      fullName = dataGridView1.Rows[dataGridView1.CurrentCellAddress.Y].Cells["fullNameDataGridViewTextBoxColumn"].Value as string;
      if (fullName != null)
      {
      MessageBox.Show(fullName);
      }
      --------------------------------------------------------------------------------------------

  • 相关阅读:
    未能写入输出文件..”“拒绝访问。”的解决办法
    SecureCRT显示中文和语法高亮
    危险无处不在 Html标签带来的安全隐患(转载)
    具有负载均衡功能的MySQL服务器集群部署及实现
    Linux以及各大发行版介绍
    yum源 redis 设置
    freebsd+apache+mysql+php+phpmyadmin+zend+discuz傻瓜式教程
    VS2005无法切换到设计视图的解决方案
    IIS不能下载EXE的解决方法
    JQuery优秀插件征集
  • 原文地址:https://www.cnblogs.com/yeye518/p/2475497.html
Copyright © 2011-2022 走看看