Reputation: 111
I've tried this code to add a row of function that I created on the main form (Form1) in DataGridView in Form2, but there is no result add row when I run its function, does anyone know where the mistake? Thanks
[edit]Code in MainForm1 :
public partial class Form1 : Form
{
Form2 sub = new Form2();
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
sub.AddRow(new string[] { "1", "2" }); // able to loop
sub.Show();
this.Enabled = false;
}
}
[edit] Code in Form2 :
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
public void AddRow(string[] values)
{
this.DGVFile.Rows.Add(values);
}
}
I get a few hints at some answers. My questions were answered when the data is loaded in datagridview in Form2 and also I want the mainform (Form1) can be disabled when opening Form2. Now it has been resolved. Thanks All.
Upvotes: 0
Views: 291
Reputation: 1151
Please review the following code:
namespace TEST_CSA
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 sub = new Form2();
sub.Show(this);
this.Enabled = false;
sub.RegisterParent(this);
for (int x = 1; x <= 5; x++)
{
string[] row;
row = new string[] { "1", "2" };
sub.DGVFile.Rows.Add(row);
}
}
}
}
The command Show(this)
makes Form1 the parent form of Form2 and also ensures that Form2 will be always displayed on top of Form1. To deactivate Form1 use this.Enabled = false;
In order to reactivate Form1 when Form2 closes use the following code:
namespace TEST_CSA
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private Form1 _parent;
internal void RegisterParent(Form1 form)
{
this._parent = form;
}
private void Form2_FormClosing(object sender, FormClosingEventArgs e)
{
_parent.Enabled = true;
}
}
}
Upvotes: 0
Reputation: 12626
Use sub.Show();
instead. This way, the code bellow sub.ShowDialog();
will not even get called.
ShowDialog
opens a new form as a modal window, which means, that the focus is only in the new window and lines below this call are called when the new windows is closed.
You can find more information about these two methods on MSDN here and here.
UPDATE:
but i want disabled mainform if open form2
I can think of two options.
The first, and in my opinion better, is to postopne opening of the form
Form2 sub = new Form2();
sub.RegisterParent(this);
for (int x = 1; x <= 5; x++ )
{
string[] row;
row = new string[] { "1","2"};
sub.DGVFile.Rows.Add(row);
}
sub.ShowDialog(); // open when everything is prepared
The second is
sub.Show();
this.Enabled = false;
but this is not really a nice solution, because you would have to enable it from the other form before closing it and there are many ways how to close a form and you would have to consider all.
Upvotes: 1
Reputation: 7830
Take the creation of the second form from the button click event out: Form2 sub = new Form2();
. This way the second form is created only once. Then use ShowDialog
and Hide
methods to show a modal second form (the first form will be inactive). Create an extra public method in the second form to add new row to the existing DataGridView.
A possible solution can look like this:
public partial class MainForm : Form
{
private DataForm dataForm = null;
public MainForm()
{
InitializeComponent();
// init data form
dataForm = new DataForm();
dataForm.Init();
}
private void buttonAddRow_Click(object sender, EventArgs e)
{
// add values
dataForm.AddRow(new string[] { "10", "20" });
// show a modal second form, so that the first can not be selected while the second is open!
dataForm.ShowDialog(this);
}
}
public partial class DataForm : Form
{
public DataForm()
{
InitializeComponent();
}
// init the grid
internal void Init()
{
this.dataGridView.Columns.Add("A", "A");
this.dataGridView.Columns.Add("B", "B");
for (int x = 1; x <= 5; x++)
{
string[] row;
row = new string[] { "1", x.ToString() };
this.dataGridView.Rows.Add(row);
}
}
// add values to the grid
public void AddRow(string[] values)
{
this.dataGridView.Rows.Add(values);
}
// hide second form
private void buttonClose_Click(object sender, EventArgs e)
{
Hide();
}
}
Output after two additions is:
Upvotes: 1
Reputation: 1151
The code after sub.ShowDialog();
is executed only after the form sub is closed. Try using sub.Show()
Upvotes: 0