Click here to Skip to main content
15,901,284 members
Please Sign up or sign in to vote.
1.67/5 (3 votes)
See more:
I have two Windows Forms (Form1 and Form2). Form1 includes one button and datagridview. Form1 shows customers list. Form2 includes 4 textbox. Form2 is my customer informations edit screen. I have 2 question :


1) When I press button which is in Form1 I want to see datas in textboxes.
2) When I change any textbox.Text content and closed Form2 ; datas which is in datagridview are not changed.When I restart project no problem all rows in datagridview are changed.

To open more when i edit the customers list i want to see it in the datagridview immediately. i want it to be refreshed and see the edited data.(by the way I tried to use datagridview.Refresh() and etc. but it doesn't worked)
Posted
Updated 15-Aug-12 22:11pm
v4
Comments
[no name] 15-Aug-12 8:27am    
Neither 1 or 2 is a question. You are really going to have to get used to the idea of posting the code that you have tried and coming up with a clear understandable question. There are plenty of ways to do this very thing and plenty of ways to screw it up.
y.baris 16-Aug-12 2:49am    
here no need codes I think. Cause I tried to write understandable question.but sorry if wrote badly that question.
y.baris 16-Aug-12 4:25am    
my codes are a litte different ..that's why I said four textbox,one button and datagridview.
I think if I solve with easy example then I will try to add in my project.

1 solution

Try this :-

First create 4 Public Static string variables in form1 class for eg.
C#
public static string text1, text2, text3, text4;

then on Form1 Button Click event write the following code:-
C#
private void button1_Click(object sender, EventArgs e)
{
    text1 = Datagridview.Rows[e.RowIndex].Cells[0].value.ToString();
    text2 = Datagridview.Rows[e.RowIndex].Cells[1].value.ToString();
    text3 = Datagridview.Rows[e.RowIndex].Cells[2].value.ToString();
    text4 = Datagridview.Rows[e.RowIndex].Cells[3].value.ToString();
}

And in Form2 write following code on form load event or create button and write in click event :-
C#
private void form2_Load(object sender, EventArgs e)
{
   TextBox1.Text = Form1.text1;
   TextBox2.Text = Form1.text2;
   TextBox3.Text = Form1.text3;
   TextBox4.Text = Form1.text4;
}

This is the Answer for your First Question.

Second Question Answer is :-
In Form2 if you change the values in Textbox then you save it. In Save Button Click event only, after the Save Code is complete, call the Form1 Load event or any other event in which you have written the code to load data in Datagridview.
In Form1 if you have written Code in Form Load event to Load data in Datagridview, then just copy that code and create One Public method like this:-
C#
public void Get_Data()
{
    //Code to Load data in Datagridview.
}

and Call this method in Form Load event like this :-
C#
private void form1_Load(object sender, EventArgs e)
{
   public void Get_Data();
}

And in same way call the same method in Form 2 Save Button Click event like this:-
First create the form object in which you have written code and access method, like this.
C#
private void Savebutton_Click(object sender, EventArgs e)
{
    //First write code to save text box data that you have already written
    Form1 f1 = new Form1();
    f1.Get_Data();
}


This is The Answer for Second Question

If need any help then plz post in comment or reply.
 
Share this answer
 
v2
Comments
y.baris 16-Aug-12 1:55am    
Thanks for your long explanation.I got two problem
first ; rowindex I cant see and thats why program is not working.
second ; I didt't understand how can I call Form1 load event in Form2.
Ank_ush 16-Aug-12 2:22am    
I need to review your code. Just show your code you have written to load data in Datagridview. I will help You. write the code to load data in datagridview in the above method in Form1 and call that same method in Form1 Load event and also in Form 2 Save Button Event after the Save code.
y.baris 16-Aug-12 2:35am    
my codes are a litte different ..that's why I said four textbox and datagridview.
I think if I solve with easy example then I will try to add in my project.
Ank_ush 16-Aug-12 2:42am    
That's ok Dear. Let me try to understand, this is important in programmers Life.
Ank_ush 16-Aug-12 3:00am    
I am watching your code that u posted in question

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