简体   繁体   English

从另一窗体更新datagridview中的选定行

[英]update selected row in datagridview from another form

I have 2 form Form1 and Form2, I want to update datagridview in Form1 from Form2 first i select the row of datagridview in Form1 then click a button to open Form2. 我有2个表单Form1和Form2,我想从Form2更新Form1中的datagridview,首先选择Form1中datagridview的行,然后单击一个按钮以打开Form2。 in Form2 I input the new data then save it. 在Form2中,我输入新数据,然后保存它。

how I can do that? 我该怎么做?

You can make a public method on Form1 to insert the row. 您可以在Form1上创建一个公共方法来插入行。 Let's call it LoadData . 我们称之为LoadData This would receive a set of parameters indicative of the data on Form2 . 这将接收一组指示Form2数据的参数。

public void LoadData( ... )
{
    // load the data into the data grid
}

Then add a new constructor to Form2 : 然后向Form2添加一个新的构造函数:

public Form2(Form1 referrer)
{
    _referrer = referrer;
}

where _referrer is a private field typed as Form1 : 其中_referrerprivate字段,键入为Form1

private Form1 _referrer;

Then when you load Form2 , pass in this because you're on Form1 : 然后,当您加载Form2 ,因为您在Form1上而传递了this

var f = new Form2(this);

Finally, when you want to add the data from Form2 , do this: 最后,当您想从Form2添加数据时,请执行以下操作:

_referrer.LoadData( ... );

My solution is similar to @Michael Perrenoud's. 我的解决方案类似于@Michael Perrenoud的解决方案。 Your purpose is to pass value from form1 to form2. 您的目的是将值从form1传递到form2。 How to pass? 如何通过? usually you need to pass the object in form1 to form2 by constructor of form2. 通常,您需要通过form2的构造函数将form1中的对象传递给form2。 then, what to pass? 然后,要通过什么? others say pass the form1 itself or pass the gridview control, but I prefer to pass the data you really want to use in form2, why? 其他人说传递form1本身或传递gridview控件,但是我更喜欢传递您真正想在form2中使用的数据,为什么呢? because when you pass a control(form or gridview), you need to analyse and get the data with it, then add to gridview2 in form2, think about that, when you pass control from form1, maybe one day you will replace the gridview by other controls such as listview or treeview, evenmore, that one day you may abandon form1, so you need to modify and Refactor your form2. 因为当您传递控件(窗体或gridview)时,您需要分析并获取数据,然后将其添加到form2中的gridview2中,请考虑一下,当您从form1传递控件时,也许有一天您将用甚至还有其他控件(例如listview或treeview),有一天您可能会放弃form1,因此需要修改和重构form2。 but if you pass the data only, you can reuse the form2. 但是,如果仅传递数据,则可以重复使用form2。 here is my sample code: 这是我的示例代码:

first, add a private field refering to your passed data 首先,添加一个私有字段来引用您传递的数据

private object mydata = null;

add a function to fill the gridview with the passed data 添加一个函数以使用传递的数据填充gridview

public void FillData( ... )
{
    if(mydata != null)
    {
    //add the data into gridview
     }
}

then, add a new constructor to Form2: 然后,向Form2添加一个新的构造函数:

public Form2(object data)
{
    _mydata = data;
}

when you want to show form2, please get the data from gridview1 当您想显示form2时,请从gridview1获取数据

void ShowData()
{
     object mydata = null;
     //get the data from selected rows and set to mydata
     Form2 f = new Form2(mydata);
     f.ShowDialog();
}

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

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