【问题标题】:Unable to get data from child form into selected Parent DataGridView Cell无法从子窗体获取数据到选定的父 DataGridView 单元格
【发布时间】:2014-07-17 15:58:33
【问题描述】:

我被一个编程问题难住了。当我右键单击 DataGridView 对象的单元格时,将显示一个上下文菜单。从该上下文菜单中,用户可以单击一个选项。该选项将显示一个表格。用户完成后,该表单的结果将填充到选定的单元格中。

问题:如何将子表单的结果放入选定的单元格?父窗体上有多个使用相同功能的 DataGridView 对象。

代码:

    private void commandOperation(Object sender, EventArgs e)
    {
        if (sender == dec2HexToolStripMenuItem)
        {
            frmNumFormatConv form = new frmNumFormatConv();
            if (form.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                // Get data from the form into the selected cell!!!
            }
        }
        else
        {
            throw new Exception("Operational object unknown");
        }
    }

    private void cellMouseDown(Object sender, DataGridViewCellMouseEventArgs e)
    {
        if (sender == dgvCircuit1TestSetup || sender == dgvCircuit2TestSetup)
        {
            if (e.Button == MouseButtons.Right)
            {
                ((DataGridView)sender).CurrentCell = ((DataGridView)sender).Rows[e.RowIndex].Cells[e.ColumnIndex];
                itsGridContextMenu.Show((DataGridView)sender, new Point(e.X, e.Y));
            }
        }
    }

【问题讨论】:

标签: c# datagridview contextmenu


【解决方案1】:

一个非常简单的方法是在弹出表单中放置一个公共属性,它将返回您想要的值(比如 RtnValue)。那么

这是弹出表单的示例:

    public string RtnValue
    {
        set { textBox1.Text = value; }
    }

这是您当前的代码:

frmNumFormatConv form = new frmNumFormatConv();
        if (form.ShowDialog() == System.Windows.Forms.DialogResult.OK)
        {
            dgvalue = form.RtnValue;
            // Get data from the form into the selected cell!!!
        }

【讨论】:

  • 但是如何将返回值放入所选 DataGridView 对象的单元格中?在 CellMouseDown 方法中,我知道我正在处理哪个 DataGridView 对象。当我使用 CommandOperation 方法时,我无法确定我来自哪个 DataGridView。这是我一直试图弄清楚的问题。我可能必须将活动的 DataGridView 对象保存为全局对象,并在完成后清除该对象。
  • 您确定该信息不在 EventArgs e 对象中吗?
  • 实际上,我希望信息在 EventArgs e 对象中。我只是不知道如何从那里提取它。是否会将 EventArgs 转换为 DataGridViewCellMouseEventArgs?
  • 不要问我这是怎么来的,但我搜索了“contextmenustrip get control”并得到了以下帖子stackoverflow.com/questions/4886327/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-12-05
  • 2013-06-14
  • 2010-10-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多