c#、winfrom点击一个窗体的datagridview中的某行数据,该数据跳转在另一个窗体的textbook中显示
窗体1:

打开时间,找到MouseDoubleClick,双击,编写代码:
private void textBox1_MouseDoubleClick(object sender, MouseEventArgs e)
{
FindSupply findSupply = new FindSupply();跳转到窗体2
findSupply.Show(this);
}
窗体2FindSupply:

点击按钮查询,然后鼠标双击datagridview一条数据在窗体一中textbook中显示
代码:
首先是button按钮,进行查询
private void button1_Click(object sender, EventArgs e)
{
string str = "select id, name as 供应商,contact as 联系人,tel as 电话号码 from Supplier where name like '%" + textBox1.Text+"%'";
SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(str, SQL);
DataTable dataTable = new DataTable();
sqlDataAdapter.Fill(dataTable);
dataGridView1.DataSource = dataTable;
dataGridView1.Columns["id"].Visible = false;
}
然后单据datagridview找到事件中的CellMouseDoubleClick,双击,这里面编写:
private void dataGridView1_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
{
int rowindex = e.RowIndex;
string b = dataGridView1.Rows[rowindex].Cells["供应商"].Value.ToString();
outSouringAdd outSouring = (outSouringAdd)this.Owner;
outSouring.Controls["textBox1"].Text = b;
this.Close();
}
最后的设置一下窗体2中的datagridview属性
1:

2:

就OK了

浙公网安备 33010602011771号