将多行从Form1的gridview传递到Form2,然后单击按钮 [英] Passing multiple rows from gridview in form1 to form2 and wait for a button click

查看:54
本文介绍了将多行从Form1的gridview传递到Form2,然后单击按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这段代码,在进行了一些更改后,将数据从form1"FrmPaint"的gridview传递到form2"OptionsPaint"到formp"FrmPaint"的Gridview2

I have this code that have passing multiple rows from gridview in form1"FrmPaint" to form2 "OptionsPaint" after making some changes passing this data to gridview2 in form1"FrmPaint"

OptionsPaint OPaint = new OptionsPaint();        
    private void btnAdd_Click(object sender, EventArgs e)
    {
        int[] selectedRows = gridView1.GetSelectedRows();
        for (int i = 0; i < selectedRows.Length; i++)
        {
            DataRow rowGridView1 = (gridView1.GetRow(selectedRows[i]) as DataRowView).Row;
            if (OPaint == null || OPaint.IsDisposed)
            OPaint = new OptionsPaint();
            OPaint.Show();

            OPaint.bsRepere.Text = rowGridView1["BS"].ToString() + " " + "Repère: " + rowGridView1["Repère"].ToString();
            OPaint.quantity.EditValue = rowGridView1["Reste Qté"];

            OPaint.OK.Click += (sender1, e1) =>
             {
                 DataRow row = dt.NewRow();
                 row[0] = rowGridView1["BS"];
                 row[1] = rowGridView1["Repère"];
                 row[2] = rowGridView1["Profil"];
                 row[3] = OPaint.quantity.EditValue;
                 dt.Rows.Add(row);
               OPaint.Close();
            }; 
         }
     }

当我只选择一行时,代码工作正常,但是当我选择多行时form2"OptionsPaint"仅显示一次,并且在gridview中从from1中选择了最后一个值.有没有一种方法可以停止代码,直到用户单击一个按钮,然后完成其余的代码.

when I select only one row the code work fine,but when I select multiple rows the form2"OptionsPaint" show only one time with the last value selectd in gridview from from1 .is there a way to stop the code until the user click a button and then complete the rest of the code .

推荐答案

这等效于您的代码:

//imagine it is a form with a single label and a button
OptionsForm of = new OptionsForm();

string[] sa = new string[]{ "a","b","c" };

foreach(string s in sa){
  of.Show();
  of.MyLabel.Text = s;
  of.MyButton.Click += (some event handler);
}

如果您有3个字符串,则此代码循环将完全运行3次:第一次运行(如果未显示 OptionsForm of ),它将显示.在已经可见的窗体上重复调用Show()不会执行任何操作.

If you have 3 strings, this code loop will run completely 3 times: the first time it runs, if the OptionsForm of is not showing it will show. Repeatedly calling Show() on a form that is already visible, does nothing.

然后在接下来的几毫秒内,标签文本将被更新3次(如此之快,您将不会看到它,特别是当您的代码正在运行时,不处理任何窗口消息),最终将其设置为"c"-您将永远不会看到"a"或"b".您的按钮单击事件将附加有3个处理程序.我不确定是否有帮助,尽管我怀疑没有用,因为在您的情况下,它将把X行放入相同的dt中.

Then over the next few milliseconds the label text will be updated 3 times (so fast you won't event see it, especially as while your code is running no window messages are processed) and will finally be set to "c" - you'll never see "a" or "b". Your button click event will have 3 handlers attached to it. I'm not entirely sure if this is helpful or not, though I suspect not, as in your case it will put X rows into dt that are identical.

这就是为什么当您选择多行时,仅显示最后一行;每个循环都将覆盖先前的循环,并且没有任何事情可以阻止循环在毫秒内完成运行

This is why when you select multiple rows only the last one shows; each loop passing overwrites the previous and nothing stops the loop running to completion in milliseconds

那该怎么办?

如果您希望此表单为每个选定项目显示一次,那么如果用户选择了10个项目,则此表单将连续出现10次,并且他们必须单击10次才能删除该表单,然后移动OPaint.在附加了按钮单击事件处理程序并将其设置为ShowDialog而不是Show之后,将Show()设置为:

If you want this form to appear once for every selected item, so if the user selected 10 items then this form will appear 10 times in a row and they have to click 10 times to get rid of it then move the OPaint.Show() to AFTER you attach the button click event handler and make it ShowDialog instead of Show:

var dResult = OPaint.ShowDialog();

要使用表单作为对话框,还需要执行一些其他设置,因为您需要声明哪个按钮是OK,哪个按钮是Cancel.请参阅MSDN上的示例

There is some additional setup you need to do to use a form as a dialog, because you need to declare which button is the OK and which is the Cancel. See this example from MSDN https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.form.dialogresult?view=netframework-4.7.2 - set the .dialogresult of your ok/cancel buttons and tell the form which is accept/cancel. Remove the instruction to Close() from the button click handler- buttons that control dialog type forms cause the form to close automatically when they're clicked

您需要一个if来测试dResult,以了解用户是单击确定"还是取消",并进行相应的处理-如果他们单击取消",我不知道要做什么?

You'll need an if to test the dResult to know if the user clicked OK or Cancel, and handle accordingly - I don't know what you'll do if they click cancel?

调用ShowDialog将导致代码在该位置停止,直到用户决定为止,就像消息框一样

Calling ShowDialog will cause the code to stop at that point until the user decides, like a messagebox does

最后,我只想指出执行此操作的用户界面非常令人讨厌.您应该考虑只有一个对话框询问可以更新所有这10行吗?".或只有一个可以同时显示和更新多个内容的表单

Finally, I just want to point out that user interfaces that do this are incredibly annoying. You should consider having just one dialog that asks "ok to update all these 10 rows?" or perhaps having a single form that can show and update multiple things at once

这篇关于将多行从Form1的gridview传递到Form2,然后单击按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆