Reputation: 2606
I just want to know if this below scenario is correct
I am trying to the following
1- from MyDataGridView1
in Form1
on duoble click on MyDataGridView1
row I am getting value of specific column and pass it to FORM2
2- assign that value to Textbox
on Form2
below event to get ti ID from datagridview
private void MyDataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
{
vID = MyDataGridView1.Rows[e.RowIndex].Cells[1].Value.ToString();
this.Close();
Form2 NewForm = new Form2();
NewForm.Show();
}
then save the value to
public static string vID ;
no Form2
Textboxid.Text = F0103.vID ;
at line below I am trying to lunch the leave event of textbox
but it is not working is there any idea to lunch leave event
BTN_Refresh.Focus();
Upvotes: 1
Views: 13371
Reputation: 864
A complete answer:
Form1:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
List<Stuff> stuff = new List<Stuff>();
stuff.Add(new Stuff() { Foo = "Foo1", Bar = "Bar1", Data = "Data1" });
stuff.Add(new Stuff() { Foo = "Foo2", Bar = "Bar2", Data = "Data2" });
var bindingList = new BindingList<Stuff>(stuff);
var source = new BindingSource(bindingList, null);
dataGridView1.DataSource = source;
}
private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
{
string arg = dataGridView1.Rows[e.RowIndex].Cells[1].Value.ToString();
Form2 form2 = new Form2(arg);
form2.Show();
this.Hide();
}
}
public class Stuff
{
public string Foo { get; set; }
public string Bar { get; set; }
public string Data { get; set; }
}
Form2:
public partial class Form2 : Form
{
public Form2(string arg)
{
InitializeComponent();
label1.Text = arg;
}
}
One thing about winforms(and why I use WPF instead) is it overly complicates this stuff. From my testing, this.Close()
closed down the entire app and I had to use this.Hide()
instead.
Also, it is good to get in the habit of passing arguments instead of setting global variables. Almost anything in C# can be passed as an argument in someway and it makes for better and cleaner code.
Upvotes: 4
Reputation: 3572
If I'm understanding your question correctly. You can pass the string to your Form2 constructor.
private void MyDataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
{
vID = MyDataGridView1.Rows[e.RowIndex].Cells[1].Value.ToString();
Form2 NewForm = new Form2(vID);
this.Close();
NewForm.Show();
}
Then in the constructor, set the TextBox
text to this string.
public class Form2
{
public Form2(string vid)
{
InitializeComponent();
Textboxid.Text = vid;
}
}
Upvotes: 0