题目为:点击form1窗体上的button 然后弹出一个form2窗体,form2窗体上有一个dataGridView1,然后双击dataGridView1的某一行数据,会把选中的该行数据传递到form1,并自动填充form1中的 textbox中.
此题关键在3点:1.窗体弹出子窗体 2.datagridview 的绑定以及双击事件 3.窗体之间的互相传值
form1弹出form2
Code
1 private void button1_Click(object sender, EventArgs e)
2 {
3 Form2 frm = new Form2();
4 frm.ShowDialog();
5 this.textBox1.Text = frm.Str;
6 this.textBox2.Text = frm.Str1;
7 } 2.datagridview 的绑定以及双击事件
Code
1 private void Form2_Load(object sender, EventArgs e)
2 {
3 SqlConnection con = new SqlConnection("Data Source=.;User ID=sa;Password=;Initial Catalog= Northwind");
4 string sql = "select * from Customers";
5 SqlDataAdapter da = new SqlDataAdapter(sql, con);
6 DataSet ds = new DataSet();
7 da.Fill(ds);
8 dataGridView1.DataSource = ds.Tables[0];
9 }
10
11 private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
12 {
13 str = this.dataGridView1[0, e.RowIndex].Value.ToString();
14
15 str1 = this.dataGridView1[1, e.RowIndex].Value.ToString();
16 this.Close();
17
18
19 } 3.窗体之间的传值这里用的是属性方法
Code
1private string str = "";
2 private string str1 = "";
3 public string Str
4 {
5 get { return str; }
6 }
7 public string Str1
8 {
9 get { return str1; }
10 }