如何将数据从一种形式的文本框传递到另一种形式的数据网格 [英] How to pass data from text box in one form to the datagrid in other form

查看:74
本文介绍了如何将数据从一种形式的文本框传递到另一种形式的数据网格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文本框和一个按钮,第二个表格网格,当单击按钮时,文本框中给出的信息应存储在数据库中,第二个表格网格应在C#中更新
如何将数据从一种形式的文本框传递到另一种形式的datagrid

i have textbox and button in form one and grid on second form,when clicked on button the information given in textbox should be stored in database, second form grid should be updated in C#
How to pass data from text box in one form to the datagrid in other form

推荐答案

我现在可以想到的几种方法:

0)将按钮设为公共变量,以便其他表单可以钩住Click事件.发送click事件时,发送事件的控件由sender参数表示.

1)创建一个公共自定义事件,该事件在单击按钮时触发,并使带有网格的窗体监视该事件的另一个窗体.您将需要一个自定义EventArgs对象来包含文本框中的文本.

我可能会选择项目1(而不是项目0).
A couple ways I can think of right now:

0) Make the button a public variable so the other form can hook the Click event. When the click event is sent, the control that sent the event is represented by the sender parameter.

1) Create a public custom event that fires when the button is clicked and have the form with the grid monitor the other form for that event. You''re going to need a custom EventArgs object to contain the text from text box.

I''d probably go with item 1 (as opposed to item 0).


选项...

1.数据绑定(Google it)
2.您的第二个表单每隔几秒钟(或每当有一次)轮询数据库以查找更改
3.您有一个静态类,该类为所有表单提供事件,以识别其他表单中的数据是否已更改
4. Form1向Form2发送一条消息,说已经更新了一些数据

哪个吸引你?

根据评论...

所以在form1的Button事件中,只需调用load数据函数就是form2

options...

1. Data Binding (Google it)
2. Your second form polls the database for changes every few seconds (or whenever)
3. You have a static class that provides events to all forms to identify if data in another form has been changed
4. Form1 sends a message to Form2 to say that is has updated some data

Which one tempts you?

Based on comment...

So in the Button event in form1 just call the load data function is form2

button1_click(..){
   //save textbox value to database
   form2.LoadData();
}



您需要在Form1中提供一个名为form2的变量,该变量是Form2的实例(或对Form2的引用)

哪个表格打开Form2?是Form1吗?

增加了更多:form2应该有一个加载数据方法,我想您已经拥有了.确保此公开.然后在form1中存储对form2的类级别引用.然后,当您单击按钮时,如果对form2的引用不为null且未处置,则会出现斑点.然后,您在form2中调用load方法.就像form2.LoadData();

....对不起,我的关系不太擅长编写代码.在PC上会在早上更新我


所以一些示例代码来说明...



You need to provide a variable in Form1 called form2 that is the instance of (or reference to) Form2

Which form opens Form2? is it Form1?

Added more: so form2 should have a load data method which I guess you already have. Make sure this isbpublic. Then in form1 you store a class level reference to your form2. then when you click button You fleck if the reference to form2 is not null and not disposed. Then you call the load method in form2. Like form2.LoadData();

....sorry my nexus one not so good for doing code. Will update I''m morning when on PC


So some sample code to illustrate...

public class Form1{
   Form2 form2;

   void OpenForm2(){
      form2 = new Form2();
      form2.Show();
   }

   void Button1_Click(object sender, EventArgs e){
      //Save the textbox values to the database

      if(form2 != null && !form2.IsDisposed)
         form2.LoadData();
   }
}

public class Form2{
   public void LoadData(){
      //This function loads you data grid data
   }
}




...希望有帮助




...hope that helps


这篇关于如何将数据从一种形式的文本框传递到另一种形式的数据网格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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