【问题标题】:Winforms - Show Datagrid selection in different windowWinforms - 在不同的窗口中显示 Datagrid 选择
【发布时间】:2014-03-04 00:25:12
【问题描述】:

我在一个窗口中有一个数据网格。

如果我双击,我需要在新窗口的文本框中显示选定的行。

当我双击一行时,我可以立即显示新表单。

我如何知道在新窗口中双击了哪一行..

我找不到让我的“form2”(即新窗口)访问发送它的数据网格的方法...

form2怎么知道..

我应该阅读哪些“主题”来理解这一点?这和数据绑定有关吗?

谢谢

【问题讨论】:

  • 在 form2 上创建一个公共方法,并在双击该行时调用它。该方法应采用您要显示的数据的参数
  • 嗨...我正在尝试 KiX 建议,然后会尝试按照您的方式工作...我想我理解了您的建议...谢谢!

标签: c# .net winforms


【解决方案1】:

为 CellDoubleClick 或 CellContentDoubleClick 定义一个事件处理程序。 在事件处理函数中执行此操作。

    private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
    { 
    DataGridViewRow row = (DataGridViewRow) sender; //cast the sender object to DataGridViewRow
    Form2 newForm = new Form2(row);
    }

在 Form2 构造函数中

    public class Form2 : Forms
    {
       private DataGridViewRow _row;

    public Form2(DataGridView row)
    {
        _row = row; // now you have a copy of the row in question
        txtFname.Text = _row.Cells[0].ToString(); 
        //row.Cells[0], row.Cells[1], row.Cells[n] will work here
    }

【讨论】:

  • 这取决于实现。但我希望我给了你一个想法。理想情况下,您只想传递有问题的行。你不需要知道整个datagridview。
  • Form2 的构造函数或 setter 方法的参数。
  • 嗨....我将如何访问 Form2 中的“行”....如果我尝试分配 txtfname = row[0].ToString();它说行需要 2 个参数.....
  • 查看我的评论。 row 在该上下文中已经可用。所以intellisense无论如何都会帮助你。 row.Cells 是该特定行中单元格的集合。所以 row.Cells[0] 将是该行的第一列。
  • 我想我误解了你。我认为更好的方法是在 Form2 类中声明一个 DataGridViewRow 字段。我会编辑它
猜你喜欢
  • 2014-03-06
  • 1970-01-01
  • 1970-01-01
  • 2018-03-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-10-05
  • 1970-01-01
相关资源
最近更新 更多