【发布时间】:2021-01-21 14:32:05
【问题描述】:
我在Form1 中有dataGridView,我需要将所选行中的数据传输到另一个表单text box。
当有人点击一行,然后点击更新按钮...
引用的数据将出现以下屏幕。
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属性创建Productspro对象。然后将pro对象传递给AddUpdateForm。像……AddUpdateForm addUpdate = new AddUpdateForm(pro);。还有其他方法可以做到这一点,并且应该有大量的谷歌搜索示例。 -
按照你说的做有两个问题。第一个,
AddUpdateForm除了pro对象。第二个是当我移动代码时,我需要一个DataGridViewCellEventArgs对象,我在updateBtn_Click中有一个EventArgs。您可以发布答案或链接解决方案。谢谢。
标签: c# winforms datagridview