C# 将数据从一个窗体上的DataGridView显示到另一个窗体上的文本框中

C# 将数据从一个窗体上的DataGridView显示到另一个窗体上的文本框中,c#,winforms,datagridview,C#,Winforms,Datagridview,我有两张表格ShoppingBasketForm.cs和EditBasketPopup.cs ShoppingBasketForm.cs在名为dataGridBasket的DataGridView中显示购物篮,该购物篮绑定到单独的ListOrderItems,该购物篮来自myOrderItem类。可通过填写产品名称数量和最新价格页面上提供的文本框/数字下拉列表,然后单击添加按钮btnAdd,将其添加到。它还可以通过单击remove按钮btnRemove从所选行中删除数据 我现在尝试实现一个Edi

我有两张表格
ShoppingBasketForm.cs
EditBasketPopup.cs

ShoppingBasketForm.cs
在名为
dataGridBasket
DataGridView
中显示购物篮,该购物篮绑定到单独的
ListOrderItems
,该购物篮来自my
OrderItem
类。可通过填写
产品名称
数量
最新价格
页面上提供的文本框/数字下拉列表,然后单击
添加
按钮
btnAdd
,将其添加到。它还可以通过单击
remove
按钮
btnRemove
从所选行中删除数据

我现在尝试实现一个
Edit
按钮
btnEdit
,单击该按钮后,它将实例化
EditBasketPopup
表单。在此表单上,将再次显示三个文本框/数字上下框
ProductName
Quantity
LatestPrice

当用户激活
btnEdit
单击事件时,如何从
dataGridBasket
上的选定行获取数据(用户无法选择单个单元格),并使用该数据填充三个文本框

我尝试在
EditBasketPopup.cs
中创建一个属性来保存第一个表单中的数据,但不确定如何以字符串形式从
dataGridBasket
中的选定行中提取数据,还遇到了需要行中每个单元格中的单个数据的问题,因为它们被绑定到不同的属性


显然,我没有太多关于这个问题的示例代码,因为这是一个关于如何而不是为什么的理论,但如果您需要任何信息,请告诉我。

@Harry Sweetman好的,首先获取datagridview的选定元素,您可以制作如下内容:

 //recover the view row
 DataGridViewRow drv = (DataGridViewRow)dataGridBasket.Current;

 //if the row is not empty => basket was selected in the dataGridView
 if (drv.Cells[0].Value != null)
 {
      //Here we get the first cell selected let say the name is like the id:
      string idBasketToEdit = drv.Cells[0].Value.ToString().Clone().ToString();

      //Now we instantiate the form EditBasketPopoup, but sending the selected basket as a parameter                  
       frmEdit = new EditBasketPopoup(idBasketToEdit);
       frmEdicion.Name = "Edit basket";
       frmEdicion.ShowDialog();         
 }
    public EditBasketPopup(string idBasket)
    {
        InitializeComponent();
        Basket b = control.GetBasket(idBasket);

        //You set the form boxes:
        txtProductName.Text = b.ProductName;
        txtLatestPrice.Text = b.LatestPrice;
        txtQuantity.Text = b.Quantity;
    }
您还需要在
EditBasketPopup.cs
中有一个表单构造函数,其篮参数如下:

 //recover the view row
 DataGridViewRow drv = (DataGridViewRow)dataGridBasket.Current;

 //if the row is not empty => basket was selected in the dataGridView
 if (drv.Cells[0].Value != null)
 {
      //Here we get the first cell selected let say the name is like the id:
      string idBasketToEdit = drv.Cells[0].Value.ToString().Clone().ToString();

      //Now we instantiate the form EditBasketPopoup, but sending the selected basket as a parameter                  
       frmEdit = new EditBasketPopoup(idBasketToEdit);
       frmEdicion.Name = "Edit basket";
       frmEdicion.ShowDialog();         
 }
    public EditBasketPopup(string idBasket)
    {
        InitializeComponent();
        Basket b = control.GetBasket(idBasket);

        //You set the form boxes:
        txtProductName.Text = b.ProductName;
        txtLatestPrice.Text = b.LatestPrice;
        txtQuantity.Text = b.Quantity;
    }
最后,假设您有一个控制器(例如BasketController),如果您有一些逻辑的话。在这里,假设您在绑定datagridview(DataGridBread)的集合上搜索要编辑的篮子


你能再解释一下你的第一段代码吗?我不太明白它是怎么工作的。谢谢您是否也可以尝试保留我仅为可读性而使用的相同变量/表单名称?
basketToEdit
Basket
从何而来?@HarrySweetman和now?这里有
control.GetBasket(idBasket)
,它应该转到您的BasketShopping控制器并搜索您需要编辑的篮子。正常吗?
Control.GetBasket(idBasket)
为我返回一个错误,显然
System.Windows.Forms.Control不包含GetBasket的定义