Click here to Skip to main content
15,886,798 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
Posted

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).
 
Share this answer
 
Comments
kkm1128 13-Apr-11 11:16am    
is there any sample code which i can follow
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();
}


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
 
Share this answer
 
v4
Comments
kkm1128 13-Apr-11 11:20am    
my second form which has grid pulls the data from database..every time i need to close the form and open it again to get updated data, when i enter something in my first form textbox, i want my second form updated and show it(without closing and opening it again
musefan 13-Apr-11 12:01pm    
See additions
kkm1128 13-Apr-11 13:46pm    
Musefan,

Yes, Form 1 opens Form2
musefan 14-Apr-11 12:30pm    
See updated

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900