Afternoon to all, I am very new to c# and need some help with the following. I currently have a form "mainForm" which contains a DataGridView "grid1" linking to a table "Table1" in SQL. From this form I call another form "addForm" to add a new record to said SQL table leaving "mainForm" in the background using the following code:
addForm frm1 = new addForm();
frm1.show();
I fill in all the details on addForm and on clicking save it saves a new record to the SQL table and and closes using:
"mainForm" which has been in the background then appears to be in focus (although I am not 100% sure an some clarification would be appreciated). I would then like to refresh "grid1" on "mainForm" automatically. Can anybody help me out? Thank you to those who took the time to read this. Gary | | G Whyman Thursday, September 10, 2009 8:43 PM | Hello Whyman, You can show frm1 as dialogue, so that the mainForm will not be in focus. Then you can put the logic to insert values in DB in frm1, and on closing form frm1 you can refresh mainForm.
It will bw something like this :
mainFrom { frm1 frm = new frm1(); frm.ShowDialogue();
RefreshMainFrom(); // to refresh mainForm }
Is this what you wanted?? Lemme know if this helps??
- Paras - Marked As Answer byAland LiMSFT, ModeratorTuesday, September 15, 2009 4:25 AM
-
| | paras kumar Thursday, September 10, 2009 10:24 PM | Note, whenever you use ShowDialog() to show a new instance of a form you should call dispose on it, or wrap it in a using statement, i.e;
mainFrom { using (frm1 frm = new frm1()) { frm.ShowDialog();
RefreshMainFrom(frm1); // to refresh mainForm } }
This only applies to forms shown with the ShowDialog command... because there is an expectation that you will refer to values and possibly controls or components in the calling code after the form is closed, so this is one of the very rare instances (perhaps the only instance) where Close is not equivalent to Dispose. When showing a form with just the Show() method you do not need to call Dispose because it is automatically called internally by the forms code when Close() is executed.
In the above example the code in RefreshMainForm would need to be able to read the values out of frm1 and add them to the underlying data source of the grid (which I presume is a data table held in a data set, from the content of the original post). If you add a row to the data table object the grid should automatically refresh it's display if it is bound correctly. In this case you'd call dataTable.Rows.Add(...) passing the values from frm1 to the add method.
- Marked As Answer byAland LiMSFT, ModeratorTuesday, September 15, 2009 4:25 AM
-
| | Yort Thursday, September 10, 2009 10:56 PM | Hi, using event handler you can achieve this
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2();
//Wired Up Here
frm2.RefreshMain += new EventHandler(frm2_RefreshMain);
frm2.ShowDialog();
}
void frm2_RefreshMain(object sender, EventArgs e)
{
//Write your logic here to update datagridview
}
private DataSet CreateDataSet()
{
DataTable table = new DataTable("childTable");
DataColumn column;
DataRow row;
DataSet dataset = new DataSet();
// Create first column and add to the DataTable.
column = new DataColumn();
column.DataType = System.Type.GetType("System.Int32");
column.ColumnName = "CustID";
column.AutoIncrement = true;
column.Caption = "ID";
column.ReadOnly = true;
column.Unique = true;
// Add the column to the DataColumnCollection.
table.Columns.Add(column);
//// Create second column.
column = new DataColumn();
column.DataType = System.Type.GetType("System.String");
column.ColumnName = "CustName";
column.AutoIncrement = false;
column.Caption = "Name";
column.ReadOnly = false;
column.Unique = false;
table.Columns.Add(column);
dataset.Tables.Add(table);
// Create three sets of DataRow objects,
// five rows each, and add to DataTable.
for (int i = 0; i <= 100; i++)
{
row = table.NewRow();
row["CustID"] = i;
row["CustName"] = "Item " + i;
table.Rows.Add(row);
}
return dataset;
}
DataSet dsCust;
private void BindData()
{
dsCust = CreateDataSet();
dataGridView1.DataSource = dsCust.Tables[0];
}
private void Form1_Load(object sender, EventArgs e)
{
BindData();
}
}
public partial class Form2 : Form
{
//Declare event handler here
public event EventHandler RefreshMain;
public Form2()
{
InitializeComponent();
}
private void Form2_Load(object sender, EventArgs e)
{
}
protected void UpdateMain()
{
if (RefreshMain != null)
RefreshMain(this, EventArgs.Empty);
}
private void Form2_FormClosing(object sender, FormClosingEventArgs e)
{
UpdateMain();
}
}
i hope it will help you
Best Regards,
C.Gnanadurai
-----------------------
Please mark the post as answer if it is helpfull to you - Marked As Answer byAland LiMSFT, ModeratorTuesday, September 15, 2009 4:25 AM
- Proposed As Answer byGnanadurai Monday, September 14, 2009 5:02 AM
-
| | Gnanadurai Friday, September 11, 2009 6:03 PM | Hello Whyman, You can show frm1 as dialogue, so that the mainForm will not be in focus. Then you can put the logic to insert values in DB in frm1, and on closing form frm1 you can refresh mainForm.
It will bw something like this :
mainFrom { frm1 frm = new frm1(); frm.ShowDialogue();
RefreshMainFrom(); // to refresh mainForm }
Is this what you wanted?? Lemme know if this helps??
- Paras - Marked As Answer byAland LiMSFT, ModeratorTuesday, September 15, 2009 4:25 AM
-
| | paras kumar Thursday, September 10, 2009 10:24 PM | Note, whenever you use ShowDialog() to show a new instance of a form you should call dispose on it, or wrap it in a using statement, i.e;
mainFrom { using (frm1 frm = new frm1()) { frm.ShowDialog();
RefreshMainFrom(frm1); // to refresh mainForm } }
This only applies to forms shown with the ShowDialog command... because there is an expectation that you will refer to values and possibly controls or components in the calling code after the form is closed, so this is one of the very rare instances (perhaps the only instance) where Close is not equivalent to Dispose. When showing a form with just the Show() method you do not need to call Dispose because it is automatically called internally by the forms code when Close() is executed.
In the above example the code in RefreshMainForm would need to be able to read the values out of frm1 and add them to the underlying data source of the grid (which I presume is a data table held in a data set, from the content of the original post). If you add a row to the data table object the grid should automatically refresh it's display if it is bound correctly. In this case you'd call dataTable.Rows.Add(...) passing the values from frm1 to the add method.
- Marked As Answer byAland LiMSFT, ModeratorTuesday, September 15, 2009 4:25 AM
-
| | Yort Thursday, September 10, 2009 10:56 PM | Thanks Yort for pointing that out.
But frm1 anyways writes the values in the table in DB, RefreshMainForm() can refresh the data from the DB also. That would be a neater approach (bit slow though :)..
anyways thanks for pointing that dispose() thingy out.
- Paras | | paras kumar Thursday, September 10, 2009 11:04 PM | Ok, in which case you'll need to pass a reference to the data table for frm1 at some point so it can write the data to it. I assume since frm1 had no reference to the data table that the form was going to return the values to the parent which did have a reference.
| | Yort Thursday, September 10, 2009 11:09 PM | Wait... I see what you're saying. You're saying write the data to the actual database instead of the data table. That's fine if they can live with the perf hit of going back to the database after every record add, and if they want to add each record to the live database after each add rather than batching up several and saving the changes to the dataset in bulk.
It would probably be more efficient to either save all the records in bulk later (i.e add them to the local dataset and then submit the dataset to the database at some later point), or add each row to the actual database and then also to the existingdata table seperately. That way there are fewer round trips to the server and the datagrid on the main form only has to display the extra row rather than re-bind the entire data table again. Of course the latter option won't work if there are identity columns or other fields with defaults on them in the database.
| | Yort Thursday, September 10, 2009 11:12 PM | Yes, that wud be a better approach to update the DB in bulk instead of going back to DB for every record add.
- Paras | | paras kumar Thursday, September 10, 2009 11:28 PM | Morning to all,
Thanks loads for the help but I am still a little lost. I have adjusted my code on "mainForm" to use the ShowDialog() command wrapping it all in a using statement as suggested to open my sub form "frm1".
I fillfrm1 out, and on closing write the data back to my database (I will look at your suggestions for better performance next :o)). I believe I am right in as far as I need to refresh my "mainForm"s datagridview "Grid1" by requerying the databasefrom "frm1" before closing but still cannot find any commands to do this.
Thanks again.
- Gary | | G Whyman Friday, September 11, 2009 8:25 AM | why do you need to query DB from frm1 to refresh mainForm. you can query the DB from mainForm also like the way its there in my first code. It would hardly matter if u refresh after closing frm1 also (unless you are concerned bout perf :).
another way is :you can pass the reference of the data source of datagridview of mainForm to frm1and modify the data source in frm1 after you are done with DB insertion. In this way you can refresh the datagrid view of mainForm before closing frm1.
Lemme know if have any question..
- Paras | | paras kumar Friday, September 11, 2009 9:37 AM | Hi, using event handler you can achieve this
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2();
//Wired Up Here
frm2.RefreshMain += new EventHandler(frm2_RefreshMain);
frm2.ShowDialog();
}
void frm2_RefreshMain(object sender, EventArgs e)
{
//Write your logic here to update datagridview
}
private DataSet CreateDataSet()
{
DataTable table = new DataTable("childTable");
DataColumn column;
DataRow row;
DataSet dataset = new DataSet();
// Create first column and add to the DataTable.
column = new DataColumn();
column.DataType = System.Type.GetType("System.Int32");
column.ColumnName = "CustID";
column.AutoIncrement = true;
column.Caption = "ID";
column.ReadOnly = true;
column.Unique = true;
// Add the column to the DataColumnCollection.
table.Columns.Add(column);
//// Create second column.
column = new DataColumn();
column.DataType = System.Type.GetType("System.String");
column.ColumnName = "CustName";
column.AutoIncrement = false;
column.Caption = "Name";
column.ReadOnly = false;
column.Unique = false;
table.Columns.Add(column);
dataset.Tables.Add(table);
// Create three sets of DataRow objects,
// five rows each, and add to DataTable.
for (int i = 0; i <= 100; i++)
{
row = table.NewRow();
row["CustID"] = i;
row["CustName"] = "Item " + i;
table.Rows.Add(row);
}
return dataset;
}
DataSet dsCust;
private void BindData()
{
dsCust = CreateDataSet();
dataGridView1.DataSource = dsCust.Tables[0];
}
private void Form1_Load(object sender, EventArgs e)
{
BindData();
}
}
public partial class Form2 : Form
{
//Declare event handler here
public event EventHandler RefreshMain;
public Form2()
{
InitializeComponent();
}
private void Form2_Load(object sender, EventArgs e)
{
}
protected void UpdateMain()
{
if (RefreshMain != null)
RefreshMain(this, EventArgs.Empty);
}
private void Form2_FormClosing(object sender, FormClosingEventArgs e)
{
UpdateMain();
}
}
i hope it will help you
Best Regards,
C.Gnanadurai
-----------------------
Please mark the post as answer if it is helpfull to you - Marked As Answer byAland LiMSFT, ModeratorTuesday, September 15, 2009 4:25 AM
- Proposed As Answer byGnanadurai Monday, September 14, 2009 5:02 AM
-
| | Gnanadurai Friday, September 11, 2009 6:03 PM | Morning everyone,
Thanks for all your help. I will post later with the solution I used in the end.
Thanks again
- Gary
| | G Whyman Monday, September 14, 2009 10:45 AM |
|