获得数据表格的价值观通行指标,供以后使用

0

的问题

我试图获得的价值观细胞的数据表格的行我点和储存起来供以后使用,但似乎我不能得到它的工作。

击行上应该做的菜单出现在这我可以选择做一个行动与这些价值观。

这是什么我已经完成为止

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs x)
        {
            if (dataGridView1.Rows[x.RowIndex].Cells["Name"].Value != null) name = dataGridView1.Rows[x.RowIndex].Cells["Name"].Value.ToString();
            else if (dataGridView1.Rows[x.RowIndex].Cells["LastName"].Value != null) last = dataGridView1.Rows[x.RowIndex].Cells["LastName"].Value.ToString();
        }

        private void dataGridView1_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                if (last != null && name != null)
                {
                    ContextMenu cm = new ContextMenu();
                    this.ContextMenu = cm;
                    cm.MenuItems.Add(new MenuItem("&Do something with those values in this row", new System.EventHandler(this.do_Action_with_values)));
                    cm.Show(this, new Point(e.X, e.Y));
                }
                last = null;
                name = null;
            }
        }

删除:如果(最后一个!= 空&&名字!= null)

将使菜单的工作,但是,价值观不是保存,他们都为空。

是否有一个适当的方式存储在字符串中所列价值观的行被点击吗?

c# datagrid
2021-11-23 17:36:12
1

最好的答案

0

每一请求的意见,利用 CellMouseClick 事件和 DataGridViewCellMouseEventArgs 结合处理程序进入一个事件处理程序的所有属性的需要。

买方注意,代码下面是,书面以外的一个IDE所以有可能是语法/其他问题。

    private void dataGridView1_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
    {
        if (e.Button != MouseButtons.Right) return;
        int row = e.RowIndex;
        string name = dataGridView1.Rows[row].Cells["Name"].Value;
        string last = dataGridView1.Rows[row].Cells["LastName"].Value;
        if (name == null || name.Trim().Length == 0) return;
        if (last == null || last.Trim().Length == 0) return;
        ContextMenu cm = new ContextMenu();
        this.ContextMenu = cm;
        cm.MenuItems.Add(new MenuItem("&Do something with those values in this row", new System.EventHandler(this.do_Action_with_values)));
        cm.Show(this, new Point(e.X, e.Y));
    }
2021-11-23 18:39:27

其他语言

此页面有其他语言版本

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................