winform form1有一个按钮弹出新窗体form2 DataGridview双击如何关闭form2?条件:1:winform 程序
2:form1上有一个按钮弹出新窗体form2,
3: form2上有一个datagridview,双击datagridview后,如何关闭form2呢?并将值返回给form1上的TextBox1呢?
2:form1上有一个按钮弹出新窗体form2,
3: form2上有一个datagridview,双击datagridview后,如何关闭form2呢?并将值返回给form1上的TextBox1呢?
控件:textBox1、button1
代码:private void button1_Click(object sender, EventArgs e)
{
Form2 fm2 = new Form2();
this.Hide();
if (fm2.ShowDialog(this) == DialogResult.Cancel)
{
textBox1.Text = fm2.Tag.ToString();
this.Show();
}
}
Form2.cs
控件:dataGridView1
代码:using System.Data.SqlClient;private void Form2_Load(object sender, EventArgs e)
{
System.Data.DataTable tableSample = getData("select top 8 * from Customers");
dataGridView1.DataSource = tableSample;
}public System.Data.DataTable getData(string strSql)
{
System.Data.DataTable dt = new System.Data.DataTable();
string strConn = "Data Source=127.0.0.1;Initial Catalog=Northwind;User ID=sa;Password=";
SqlConnection conn = new SqlConnection(strConn);
conn.Open();
try
{
SqlCommand comm = new SqlCommand();
comm.Connection = conn;
comm.CommandText = strSql;
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = comm;
da.Fill(dt);
}
catch (Exception ex)
{
}
finally
{
conn.Close();
}
return dt;
}private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
{
this.Tag = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();
this.DialogResult = DialogResult.Cancel;
Close();
}
public string strResult="";datagrid.doubleclick+=........datagrid_doubleclickprotected void datagrid_doubleclick(...sender, ...e)
{
strResult=datagrid.selectitem.xxxx.tostring();
this.close();
}form1 :form2 f2=new form2();
f2.closed+=........form_close
void form_close(...sender, ...e)
{
string str=((form2)sender).strResult;
}