如何在不打开新winform的情况下将值从gridview传递到打开的表单 [英] how to pass values from gridview to opened form without opening new winform
问题描述
嘿伙计们,
我有一点问题,因为我是个书呆子。我有这个任务要做。
i有一个表单来查找客户详细信息,并在gridview上双击所选客户,其详细信息应该传递给打开winform,但据我所知只有frm.show或frm.showdialogue打开了一个全新的winform,这不是我想要的。请帮帮我.......谢谢
i这里的代码如下
Form_OrderEntry frm = new Form_OrderEntry();
frm.txtcustomercode.Text = dataGridView1.CurrentRow.Cells [ 0 ]。Value.ToString();
frm.txtcustomername.Text = dataGridView1.CurrentRow.Cells [ 1 ]。Value.ToString();
frm.Visible = true ;
此 .Close();
form_orderentry是我的主要表单,它应该在其customercode和name文本框中获取值。但是我得到了一个全新的窗口。
在你的dataGridViewCell_Click事件中写下{}块的代码,如下所示
public void dataGridView1_CellClick( object sender,DataGridViewCellEventArgs e)
{
frm.txtcustomercode.Text = dataGridView1.Rows [e.RowIndex] .Cells [ ColumnNameOf_CustomerCode跨度>] Value.ToString();
frm.txtcustomername.Text = dataGridView1.Rows [e.RowIndex] .Cells [ ColumnNameOf_CustomerName跨度>] Value.ToString();
}
在orderenter表单中有一个公共静态变量,像这样找到opended orderenter
使用FormCollection类获取打开的表单对象。
FormCollection fc = Application.OpenForms;
foreach (表格frm in fc)
{
if (frm.name == orderenter )
{
frm.Myvalue = customerdetails;
}
}
或
有公共静态方法在orderenter中传递值作为参数
FormCollection fc = Application.OpenForms;
foreach (表格frm in fc)
{
if (frm.name == orderenter )
{
frm.showform(customerdetails);
}
// 在orderenter表格中有show showform method
public static void showform(customerdetails)
{
txtcustomercode.Text = customerdetails;
}
}
据我所知,
Form1 ---> Form2(现在当它关闭传递结果再次打开form1时)
你应该用你的形式做到这一点2
- >在 form2(其中你有写方法 声明表单级公共变量 Return_Cust_code,Return_Cust_Nm
- >现在设置变量在表单关闭方法而不是您提到的新表单开放代码
Return_Cust_code = dataGridView1.CurrentRow.Cells [ 0 ]。Value.ToString();
Return_Cust_Nm = dataGridView1.CurrentRow.Cells [ 1 ]。Value.ToString() ;
this .DialogResult = Dialogresult.ok;
this .Close() ;
- >现在 Form1 收到这些varibles 就像这样
< pre lang =c#> Form1 frm = new Form1();
if (frm .ShowDialog()== Dilogresult.OK)
{
string cc = frm.Return_Cust_code;
串跨度> cn = frm.Return_Cust_Nm;
}
快乐编码!
:)
Hey guys,
i have a little problem as i m a nerd. i have this task to do.
i have a form to find the customer details and on the double click of the selected customer on gridview, its details should pass into the opened winform but as i know only frm.show or frm.showdialogue which opens a complete new winform which is not what i have wanted . please help me out.......thanks
i have a code here like this
Form_OrderEntry frm = new Form_OrderEntry();
frm.txtcustomercode.Text = dataGridView1.CurrentRow.Cells[0].Value.ToString();
frm.txtcustomername.Text = dataGridView1.CurrentRow.Cells[1].Value.ToString();
frm.Visible = true;
this.Close();
form_orderentry is my main form and it should get values in its customercode and name textbox. but i get a complete new window.
On ur dataGridViewCell_Click event write the code of { } block,given below
public void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e) { frm.txtcustomercode.Text = dataGridView1.Rows[e.RowIndex].Cells["ColumnNameOf_CustomerCode"].Value.ToString(); frm.txtcustomername.Text =dataGridView1.Rows[e.RowIndex].Cells["ColumnNameOf_CustomerName"].Value.ToString(); }
Have a public static variable in the orderenter form,Find the opended orderenter by like this
Use FormCollection class to get the openned form object.
FormCollection fc = Application.OpenForms; foreach (Form frm in fc) { if(frm.name=="orderenter") { frm.Myvalue= customerdetails; } }
or
Have public static method in orderenter and pass the value as parameter
FormCollection fc = Application.OpenForms; foreach (Form frm in fc) { if(frm.name=="orderenter") { frm.showform(customerdetails); } // In the orderenter form have show showform method public static void showform(customerdetails) { txtcustomercode.Text =customerdetails; } }
As I understand,
Form1 ---> Form2 (now when it close pass result to already opened form1 again)
you should do this in your form2
-> In form2 (in which you have write methoddeclare form level public variables Return_Cust_code,Return_Cust_Nm
-> now set that variables in form close method instead of that new form opening code you have mention in question
Return_Cust_code = dataGridView1.CurrentRow.Cells[0].Value.ToString(); Return_Cust_Nm = dataGridView1.CurrentRow.Cells[1].Value.ToString(); this.DialogResult= Dialogresult.ok; this.Close();
-> now in Form1 receive these varibles like this
Form1 frm = new Form1(); if (frm.ShowDialog() == Dilogresult.OK) { string cc = frm.Return_Cust_code; string cn = frm.Return_Cust_Nm; }
Happy Coding!
:)
这篇关于如何在不打开新winform的情况下将值从gridview传递到打开的表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!