使用ShowDialog()在C#中使用DataGridView打开窗体(Using ShowDialog() to open form with a DataGridView in C#)

编程入门 行业动态 更新时间:2024-10-28 02:22:15
使用ShowDialog()在C#中使用DataGridView打开窗体(Using ShowDialog() to open form with a DataGridView in C#)

所以,我有一个应用程序有两种形式,一种是主要的,另一种是我按F1时调用的,它会转到另一种带有DataGridView形式。 它将打开第二个窗体作为新窗体,但我希望用Form2.ShowDialog();打开窗体Form2.ShowDialog(); 而不是Form2.Show(); 。

这个新闻F1事件正在主窗体的TextBox上使用,并且在向TextBox插入内容后,它将根据TextBox中写入的内容在我的数据库中搜索记录。

它适用于如果我使用Form2.Show(); ,但如果我更改为Form2.ShowDialog(); 它不会处理数据,并且DataGridView将显示为空而不显示任何记录。

这是我在主要获得的:

private void txtCarga_KeyDown(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.F1) { frmPesquisa frmP = new frmPesquisa(this); frmP.Show(); con = new SqlConnection(cs.DBConnP); con.Open(); //The rest of the code is calling the query }

然后,在第二种形式中,我有这个:

private frmPrincipal frm; public frmPesquisa(frmPrincipal frm) : this() { this.frm = frm; }

我能做些什么来使用Form2.ShowDialog();在第二种形式显示DataGridView Form2.ShowDialog(); ?

So, I have an application with two forms and one of the main one and the other one is called when I press F1 and it goes to another form with a DataGridView. It will open the second form as a new form, but I want it to be opened with Form2.ShowDialog(); instead Form2.Show();.

This press F1 event is being used on the TextBox from the main form and after inserting something into that TextBox it will search in my database the records depending on what was written in the TextBox.

It works if I use Form2.Show();, but if I change to Form2.ShowDialog(); it won't process data and the DataGridView will be empty without displaying any records.

This is what I got in the main for:

private void txtCarga_KeyDown(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.F1) { frmPesquisa frmP = new frmPesquisa(this); frmP.Show(); con = new SqlConnection(cs.DBConnP); con.Open(); //The rest of the code is calling the query }

Then, in the second form I have this:

private frmPrincipal frm; public frmPesquisa(frmPrincipal frm) : this() { this.frm = frm; }

What can I do to display the DataGridView in the second form using Form2.ShowDialog();?

最满意答案

ShowDialog以模态方式显示窗口,因此在子窗体打开时它是一个阻塞调用:

您可以使用此方法在应用程序中显示模态对话框。 当调用此方法时,直到关闭对话框之后,它后面的代码才会执行​​。

这意味着在表单关闭之前您并未填充DataGridView。

调用ShowDialog 之前,您需要在Form2上填充DataGridView和其他任何控件。

ShowDialog shows the window modally, so it's a blocking call while the child form is open:

You can use this method to display a modal dialog box in your application. When this method is called, the code following it is not executed until after the dialog box is closed.

This means you're not populating the DataGridView until after the form has been closed.

You need to populate the DataGridView and any other controls on Form2 before calling ShowDialog.

更多推荐

本文发布于:2023-04-27 23:58:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1329617.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:窗体   DataGridView   ShowDialog   open   form

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!