先上效果图,点击UserName这一列的某一单元格,右边的textbox文本框显示datagridview控件点击的值

代码:

private void btnSea_Click(object sender, EventArgs e)
{
SqlConnection sqlConnection = new SqlConnection();
sqlConnection.ConnectionString = "Server=(local);Database=OnLine;Integrated Security=sspi";
SqlCommand sqlCommand = new SqlCommand();
sqlCommand.Connection = sqlConnection;
sqlCommand.CommandText = "select * from tb_Mouth";
SqlDataAdapter sqlDataAdapter = new SqlDataAdapter();
sqlDataAdapter.SelectCommand = sqlCommand;
DataTable MouthTable = new DataTable();
sqlConnection.Open();
sqlDataAdapter.Fill(MouthTable);
sqlConnection.Close();
this.dataGridView1.DataSource = MouthTable;
textBox1.Text = dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells["UserName"].Value.ToString();
}
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
textBox1.Text = dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells["UserName"].Value.ToString();
}
本文展示了一个使用C#实现DataGridView控件与TextBox文本框联动的示例。当用户点击DataGridView中UserName列的某个单元格时,对应的值会自动显示在右侧的TextBox中。代码详细介绍了如何从数据库获取数据并填充DataGridView,同时监听单元格点击事件来更新TextBox的内容。
911

被折叠的 条评论
为什么被折叠?



