【问题标题】:transfer data from one form dataGridView to another form TextBox将数据从一个表单 dataGridView 传输到另一个表单 TextBox
【发布时间】:2021-01-21 14:32:05
【问题描述】:

我在Form1 中有dataGridView,我需要将所选行中的数据传输到另一个表单text box。 当有人点击一行,然后点击更新按钮...

FORM1

引用的数据将出现以下屏幕。

AddUpdateForm 以下代码用于UPDATE 按钮

 private void updateBtn_Click(object sender, EventArgs e)
 {

             AddUpdateForm addUpdate = new AddUpdateForm();
             addUpdate.ShowDialog();
 }

此代码用于dataGridViewPRoducts上的点击事件

   private void dataGridViewProducts_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            AddUpdateForm addUpdate = new AddUpdateForm();
            Products pro = new Products();
            if (e.RowIndex >= 0)
            {
                DataGridViewRow row = dataGridViewProducts.Rows[e.RowIndex];
                pro.Name = row.Cells["name"].Value.ToString();
                pro.Brand = row.Cells["brand"].Value.ToString();
                pro.Item_Price = Convert.ToDouble(row.Cells["item_price"].Value.ToString());
                pro.StokType =row.Cells["stock_type"].Value.ToString();
                pro.Stock_Amount = Convert.ToDouble(row.Cells["stock_amount"].Value.ToString());
                textBox1.Text = row.Cells["name"].Value.ToString();
            }
         

实际上我知道我需要将pro 对象返回给UPDATE button。但是,我不知道该怎么做。

【问题讨论】:

  • CellContentClick事件中的代码移动到updateBtn_Click事件中。使用网格 CurrentRow 属性创建 Products pro 对象。然后将pro 对象传递给AddUpdateForm。像……AddUpdateForm addUpdate = new AddUpdateForm(pro);。还有其他方法可以做到这一点,并且应该有大量的谷歌搜索示例。
  • 按照你说的做有两个问题。第一个,AddUpdateForm 除了 pro 对象。第二个是当我移动代码时,我需要一个DataGridViewCellEventArgs 对象,我在updateBtn_Click 中有一个EventArgs。您可以发布答案或链接解决方案。谢谢。

标签: c# winforms datagridview


【解决方案1】:

根据您对这两个问题的评论……

“第一个,AddUpdateForm 不只是一个 pro 对象。”

这是真的,我的评论暗示您应该创建一个“确实”接受 pro 对象的构造函数。然后表单可以访问传入的Products 对象。像……

private Products currentProduct;

public AddUpdateForm(Products passedInProduct) {
  currentProduct = passedInProduct;
  InitializeComponent();
}

private void AddUpdateForm_Load(object sender, EventArgs e) {
  // here you can use the passed in Products object called currentProduct
}

您的第二条评论…

”第二个是当我移动代码时,我需要一个 DataGridViewCellEventArgs 对象,其中有一个 EventArgs updateBtn_Click。”

这不是真的。您不需要 DataGridViewCellEventArgs 对象。在当前状态下,通过在网格中设置pro 对象CellContentClick 事件意味着只有当用户“点击”一个单元格时,pro 对象才会被设置……如果用户按下向下箭头而不是点击?如果用户从不点击单元格,pro 将为空。

无论用户如何“选择”它,您都想抓取当前“选择”的行。所以在用户按下“更新”按钮之前抓取该行是没有意义的。然后我们只需选择网格@987654328 @…即…当前选定行的网格。

因此,更新按钮点击事件可能看起来像……

private void updateBtn_Click(object sender, EventArgs e) {
  DataGridViewRow row = dataGridViewReport.CurrentRow;
  Products pro = new Products {
    Name = row.Cells["name"].Value.ToString(),
    Brand = row.Cells["brand"].Value.ToString(),
    Item_Price = Convert.ToDouble(row.Cells["item_price"].Value.ToString()),
    StokType = row.Cells["StokType"].Value.ToString(),
    Stock_Amount = Convert.ToDouble(row.Cells["Stock_Amount"].Value.ToString())
  };
  AddUpdateForm addUpdate = new AddUpdateForm(pro);
  addUpdate.ShowDialog();
}

或者,如果网格数据源是 List<Products>,那么您可以从行中获取 DataBoundItem 并将其转换为 Products 对象,而不是从单个单元格中获取值...

Products pro = (Products)dataGridViewReport.CurrentRow.DataBoundItem;

我希望这是有道理的。

【讨论】:

  • 非常感谢它完美运行。我很欣赏它。
猜你喜欢
  • 1970-01-01
  • 2017-06-01
  • 1970-01-01
  • 1970-01-01
  • 2014-10-31
  • 1970-01-01
  • 2015-12-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多