先上效果图,点击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();
}