简体   繁体   English

将数据从一个窗体上的DataGridView显示到另一窗体上的TextBoxes中

[英]Displaying data from a DataGridView on one Form into TextBoxes on another

I have two forms. 我有两种形式。 ShoppingBasketForm.cs and EditBasketPopup.cs . ShoppingBasketForm.csEditBasketPopup.cs

ShoppingBasketForm.cs displays a shopping basket in a DataGridView called dataGridBasket which is bound to a seperate List<OrderItem>OrderItems which comes from my OrderItem class. ShoppingBasketForm.cs在名为dataGridBasketDataGridView显示一个购物篮,该购物篮绑定到来自我的OrderItem类的单独的List<OrderItem>OrderItems This can be added to by filling in the provided text boxes/numericUpDowns on the page Product Name Quantity and LatestPrice and then clicking the Add button btnAdd . 可以通过在“ Product Name Quantity和“ LatestPrice ”页面上填写提供的文本框/ numericUpDowns,然后单击“ Add按钮btnAddAdd It also has the ability to remove data from the selected row by clicking the Remove button btnRemove . 通过单击“ Remove按钮btnRemove它也可以从所选行中删除数据。

I'm now trying to implement an Edit button btnEdit where once clicked, it instantiates the EditBasketPopup form. 我现在正在尝试实现一个btnEdit Edit按钮,单击该按钮将实例化EditBasketPopup表单。 On this form there are the three text boxes/ numeric up downs displayed again ProductName Quantity and LatestPrice . 在此表单上,将再次显示三个文本框/数字下标ProductName QuantityLatestPrice

How do I take the data from the selected row on the dataGridBasket (User cannot select single cells) and use that to fill the three text boxes when the user activates the btnEdit click event? 我如何从dataGridBasket上的选定行中获取数据(用户无法选择单个单元格),并在用户激活btnEdit click事件时使用它填充三个文本框?

I tried creating a properties inside the EditBasketPopup.cs to hold the data from the first form, but was unsure how to extract the data from the selected row in the dataGridBasket in string form and also came across the problem of needing the individual data from each cell in the row, seeing as they are bound to different properties. 我尝试在EditBasketPopup.cs创建一个属性来保存第一个表单中的数据,但是不确定如何从dataGridBasket中的选定行中以字符串形式提取数据,并且还遇到了需要从每个表单中获取单独数据的问题该行中的单元格,因为它们绑定到不同的属性。

I obviously don't have much example code for this question as it's a theory as to how rather than why but if you need anything just let me know. 对于这个问题,我显然没有太多示例代码,因为这是关于如何而不是为什么的理论,但是如果您需要任何信息,请告诉我。

@Harry Sweetman ok first to get the selected element of your datagridview you can make something like this: @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();         
 }

Also you need to have a form constructor in EditBasketPopup.cs with a Basket parameter like this: 另外,您还需要在EditBasketPopup.cs具有一个带有Basket参数的表单构造函数,如下所示:

    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;
    }

Finally let s suppose you have a controller (Ex. BasketController) were you have some logic. 最后,假设您有一个逻辑(例如,BasketController)。 Here it 's supposed that you search the Basket you want to edit on the collection that it 's binding your datagridview (dataGridBasket). 在这里,假设您在绑定了datagridview(dataGridBasket)的集合中搜索要编辑的Basket。

public Basket GetBasket(string idBasket)
{
    try
    {
        //basketCollection is the one that you use to bind dataGridBasket
        return basketCollection.Find(x => x.idBasket.Equals(idBasket));
    }
    catch (Exception ex)
    {
        throw ex;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM