如何在另一个表单中的DatagridView中显示数据结果 [英] How to display data results in DatagridView in another Form
问题描述
Hi Fellows
如果单击一个按钮,我很难在另一个具有DataGridView的表单中显示我的数据信息
要在dgv中以相同的形式显示结果不是问题,但在dgv中以表格2显示它让我头疼(:
例如:
Form1
cmd.commandText:SELECT * FROM tblproducts
void btnDisplay_Click(object sender,EventArgs e)
if(comboBox1.SelectedValue.ToString()!= null& & comboBox2.SelectedValue.ToString()!= null)
试试
打开连接
新Form2 ()。显示对话();
??????下一个什么?
Form2
dataGridView1.DataSource =从Form1获取信息;
可以sb help或任何例子pleeaaaaseeee?
Hi Fellows
I'm having a tough time trying to display my data information in another form which has a DataGridView, if a button is clicked
To display the results in the same Form in dgv isn't a problem but to display it in Form 2 in a dgv is giving me a headache (:
Example:
Form1
cmd.commandText: SELECT * FROM tblproducts
void btnDisplay_Click(object sender, EventArgs e)
if (comboBox1.SelectedValue.ToString() != null && comboBox2.SelectedValue.ToString() != null)
try
open connection
new Form2().ShowDialog();
?????? what next??
Form2
dataGridView1.DataSource = get Info from Form1;
Can sb help or any examples pleeaaaaseeee?
推荐答案
为什么不穿你在项目中添加了一个静态类,在Form1中填充它,然后在保存对象列表的静态类中添加一个属性,然后将它绑定到Form2中的datagridview吗?
Why don't you add a static class to your project, populate this in Form1 then add a property to the static class holding your list of objects, then bind this to your datagridview in Form2??
基本思想是传递一些数据,任何数据。无论是在datagridview还是文本框中填充它都不重要。我从来没有真正使用过Windows窗体,但这肯定会给你一些想法。
尝试在表单之间传递数据
请记住,您可能需要填充大量数据第二种形式,但这并不意味着你必须从第一种形式发送所有这些。就像从数据库中获取记录一样,将主键从form1发送到form2,然后在form2中,使用该pkey获取所有记录。
希望这会有所帮助。
热烈的问候,
Subho
The basic idea is to pass some data, any data. Whether you populate that in a datagridview or a textbox shouldn't really matter. I have never really worked with windows forms but this will definitely give you some ideas.
Try Passing Data Between Forms
Keep in mind that you might need to populate a lot of data in the second form but that doesn't mean you have to send all that from the first form. Like if you are fetching records from a DB, send the primary key from form1 to form2 and in form2, fetch all the records using that pkey.
Hope this helps.
Warm regards,
Subho
好吧,只需在Form 2中添加第二个构造函数并从那里填充它......更容易
Ok just add a second constructor to Form 2 and populate it from there..... much easier
public partial class Form2 : Form
{
string _passValue;
public string Passvalue
{
get { return _passValue; }
set { _passValue = value; }
}
public Form2()
{
InitializeComponent();
}
public Form2(string Data)
{
_passValue = Data;
}
}
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
Form2 _form2 = new Form2(dataGridView1.DataSource);
_form2.ShowDialog();
}
}
这篇关于如何在另一个表单中的DatagridView中显示数据结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!