将数据显示为数据集返回的其他形式的数据 [英] Displaying data into other form that return by the Dataset

查看:67
本文介绍了将数据显示为数据集返回的其他形式的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,
我需要你的帮助.

您对如何将数据显示为数据集返回的其他形式有任何想法吗?我在第二种形式中使用了datagridview,我想显示在第一种形式中输入的数据.我在此应用程序中与数据库MySQL服务器一起使用c#.

谢谢,
Nico

Hi All,
I need your help.

Do you have any idea on how to display your data into other form that return by the Dataset? I used datagridview in my second form, and I want to display the data that I input in the first form. I''m using c# in this application with the Database MySQL server.

Thanks,
Nico

推荐答案

何时应在datagridview中加载数据?两种形式又有什么关系?这就是答案的关键.

1.如果form1打开form2,最简单的方法可能是将数据集/数据表作为构造函数参数传递给form2.可以用作gridview的数据源.
2. Form2还可以公开由form1设置的datagrdiview数据源的属性.
3.如果在form2中发生任何事件,最好将form2设为一个公共事件,该事件应由form1处理.

更新:我可以想到两种方法. Form1有一个DataSet变量,该变量发送到form2的构造函数.现在,在form2中,保存完成后,将新数据集设置为此变量.然后,您可以在form1中使用它.像这样的东西:

When should the data load in the datagridview? And how are both the forms related? That holds the key to the answer.

1. If form1 opens form2, simplest way could be to either pass the dataset/datatable as constructor parameter to form2. This can be used as the datasource for the gridview.
2. Form2 can also expose a property for the datagrdiview datasource which can be set by form1.
3. If there is any event in form2 on which this happens, preferably, form2 must make it a public event which should be handled by the form1.

Update: I can think of two ways of doing this. Form1 has a DataSet variable which is sent to constructor of form2. Now, in form2, after save is done, set the new dataset to this variable. Then you can use it in the form1. Something like this:

DataSet dataSet = new DataSet();
Form2 form2 = new  Form2(dataSet);



在form2中:



In form2:

class Form2{

DataSet _dataSet = new DataSet();

Form2(DataSet dataSet){
_dataSet = dataSet;
}
}



保存完成后:



After save is done:

_dataSet = newDataSet;



因此,现在,从Form1传递的变量dataSet应该具有来自Form2的值.

这是执行此操作的另一种方法:

假设Form1打开Form2,并说您有一个保存"按钮以将数据保存在数据库中.我会这样做:

在Form2中:



So now, the variable dataSet that was passed from Form1 should have values from Form2.

Here is another way of doing this:

Assuming Form1 opens Form2 and say you have a Save button to save the data in the database. I will do it this way:

In the Form2:

class Form2{
public event EventHandler SaveButtonClick;

protected virtual void OnSaveButtonClick(EventArgs e){
if(SaveButtonClick != null){
SaveButtonClick(btnSave, e);
}

//Suppose this is the click event handler for the save button in form2
private void ClickSaveButton(object sender, EventArgs e){
// Do some work if needed
OnSaveButtonClick(e);
}
}
}



在Form1中,此代码应遵循您在创建和显示Form2的行:



In the Form1, this code should follow the lines where you are creating and displaying the Form2:

Form2 form2 = new Form2();
form2.SaveButtonClick += new EventHandler(Form2SaveClick);



现在,您应该在form1中拥有方法Form2SaveClick,当在Form2上单击保存"按钮时,将触发该方法.这是执行流程的发生方式:

点击在Form2上保存-> ClickSaveButton-> OnSaveButtonClick-> Form2SaveClick

我给出的示例有一个EventArgs作为参数.如果将其更改为自定义类型,则可以使用该类型共享数据集.也就是说,Form2SaveClick中的参数"e"应具有一个名为DataSet的属性,该属性将来自于form2.因此,所有保存将在调用Form2中的OnSaveButtonClick之前完成.然后,应为OnSaveButtonClick的"e.DataSet"分配新的数据集.


注意:我已经在此处编写了所有代码,因此不确定该工作是否可行,甚至无法编译.如果错误,具有VS访问权限的人可以纠正问题.



Now you should have the method Form2SaveClick in the form1 which will be fired when the Save button is clicked on the Form2. This is how execution flow will happen:

Click Save on Form2 -> ClickSaveButton -> OnSaveButtonClick -> Form2SaveClick

Example I have given has a EventArgs as argument. If you change it to your custom type, you can share the dataset using that. ie the argument "e" in the Form2SaveClick should have a property called DataSet which will come from form2. So all the save will be done before calling OnSaveButtonClick in the Form2. And then, "e.DataSet" of OnSaveButtonClick should be assigned the new dataset.


NOTE: I have written all the code here itself so I am not sure whether this work work or even compile. Someone with VS access can correct things if it''s wrong.


在Form1触发该事件时,您需要使用事件来触发该事件.
然后在您的Form2中订阅该事件,然后触发该操作,您将获得该DataSet并在该Form2中使用它.
我希望这能给您一个想法.

http://stackoverflow.com/questions/3842505/call-event-from-form2- in-form1 [^ ]
You need to use events to trigger an event when you do something in your Form1 triger that event.
And in your Form2 subscribe to that event, and when the action is trigered you will get that DataSet and use it in that Form2.
I hope this gives you an idea.

http://stackoverflow.com/questions/3842505/call-event-from-form2-in-form1[^]




在表单2中,当您成功将填写的表单详细信息提交到数据库return scopeIdentity时,它将返回您最后插入的主键值,然后将其从表单2重定向到表单1,并在querystring中传递该主键值,然后在页面加载中传递该主键值.表格1检查Querystring不为null,然后根据该querystring值从数据集中的数据库获取数据并将其绑定到GridView
Hi,

In form 2 when you successfully submitted your fill up form details into the Database return scopeIdentity which will return you the last primary key inserted value then redirect it from Form 2 to form 1 and pass that primary key value in querystring then in the page load of form 1 check Querystring is not null then on the basis of that querystring value get the data from the database in the dataset and bind it to your GridView


这篇关于将数据显示为数据集返回的其他形式的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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