Click to See Complete Forum and Search --> : Refresh winform
HappyTomato
July 18th, 2005, 08:57 AM
hi all.. thanks in advance for helping :)
we have a parent form with datagrid which opens a second form to edit this datagrid. how can we refresh the datagrid when the user has closed the second form after editing? We've tried reloading and/or refreshing both the datagrid and form and opening and closing forms but none seemed to work... >_< any help will be grateful. :)
exterminator
July 18th, 2005, 09:24 AM
On the OnFocus/GotFocus event of the form containing the datagrid, call a refresh for the datagrid. Thats it ! :thumb ... Oh..and yes try handling the scenarios when your first form got focus apart from this case. You would not want a refresh if you did not open the form to edit information on the datagrid and came back to the original form , for ex - no refresh required when i opened someother form and came back to this one..or simply the focus came out of may be different applications running at the same time. Hope I am clear.
HappyTomato
July 18th, 2005, 10:12 AM
i see what you mean, we only have this parent form and the second form to deal with, but where should i be putting the code for it to work? and how can we trigger the actual event to check whether the parent form has focus? e.g. idealing it should be checking whether the parent window has focus when user closes the second window, is that right? so how can we do that? thanks again... :)
jhammer
July 18th, 2005, 10:41 AM
I did the following and it worked:
I created a project with two forms:Form1 (main) and Form2.
Form1 has a DataGrid and a Button (that opens Form2)
Form2 has a DataGrid and a Button (that closes Form2)
Form1 has a private member DataTable. In the constructor:
public Form1()
{
InitializeComponent();
InitializeTable();
dataGrid1.DataSource = _table;
}
private DataTable _table;
private void InitializeTable()
{
_table = new DataTable("xxx");
_table.Columns.Add(new DataColumn("col1", typeof(string)));
_table.Columns.Add(new DataColumn("col2", typeof(string)));
_table.Columns.Add(new DataColumn("col3", typeof(string)));
DataRow dr = _table.NewRow();
dr["col1"] = "x"; dr["col2"] = "y"; dr["col3"] = "z";
_table.Rows.Add(dr);
dr = _table.NewRow();
dr["col1"] = "1"; dr["col2"] = "2"; dr["col3"] = "3";
_table.Rows.Add(dr);
dr = _table.NewRow();
dr["col1"] = "a"; dr["col2"] = "b"; dr["col3"] = "c";
_table.Rows.Add(dr);
}
In the event handler for the button in Form1 I put:
private void button1_Click(object sender, System.EventArgs e)
{
Form2 frm = new Form2(_table);
frm.ShowDialog();
}
In Form2 constructor I get the table from Form1. It is the same table (reference is passed):
private DataTable _table;
public Form2(DataTable dt)
{
InitializeComponent();
_table = dt;
dataGrid1.DataSource = _table;
}
And the Button in Form2 only closes the form:
private void button1_Click(object sender, System.EventArgs e)
{
this.Close();
}
Now when I run the program I see the table in the grid in Form1. Pressing the button will open Form2 with the same data. I can change the data, and when I press the button in Form2, it is closed and the changes go back to Form1.
I don't need any GotFocus or LostFocus events.
exterminator
July 19th, 2005, 01:46 AM
Now when I run the program I see the table in the grid in Form1. Pressing the button will open Form2 with the same data. I can change the data, and when I press the button in Form2, it is closed and the changes go back to Form1.
I don't need any GotFocus or LostFocus events.Hmm...Nice..HappyTomato can go ahead with this methodology. :thumb:
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.