如何将数据从一种形式的文本框传递到另一种形式的数据网格

如何将数据从一种形式的文本框传递到另一种形式的数据网格

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

问题描述

我有一个文本框和一个按钮,第二个表格网格,当单击按钮时,文本框中给出的信息应存储在数据库中,第二个表格网格应在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

推荐答案


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


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

08-14 05:08