您的位置:首页 > 编程语言 > C#

C#窗体dataGridView控件鼠标双击显示信息

2018-04-07 17:01 881 查看
dataGridView控件常用来显示数据库表的信息。现在我们如何实现当我们用鼠标双击dataGridView控件中的某一行时,把该行的信息显示出来?举例操作如下图:



/*
假设dataGridView控件已经能够成功加载并且正常显示
*/

private void DataGridViewCellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
{
if (e.RowIndex >= 0) {
DataTable table = (DataTable)dataGridView1.DataSource;//数据源
string id = table.Rows[e.RowIndex]["药品编号"].ToString();//获取表的列名(id)
using (SqlDataReader reader = cmd.ExecuteReader())
{
while (reader.Read())//遍历表
{
if (id.ToString()==reader["药品编号"].ToString())//显示
{
textBox1.Text =reader["药品编号"].ToString();//查找成功,刷新值
textBox2.Text = reader["名称"].ToString(); //规格
textBox3.Text = reader["规格"].ToString();
textBox4.Text = reader["整量单位"].ToString(); //
textBox5.Text = reader["散量单位"].ToString(); //
textBox6.Text = reader["入库单价"].ToString(); //
textBox7.Text = reader["出库单价"].ToString(); //
textBox8.Text = reader["分类"].ToString(); //
textBox9.Text = reader["有效期"].ToString(); //
textBox10.Text = reader["库存数量"].ToString(); //
textBox11.Text = reader["拼音码"].ToString(); //
textBox12.Text = reader["是否处方药"].ToString(); //
}
}
}
}
}
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
this.dataGridView1.CellMouseDoubleClick += new System.Windows.Forms.DataGridViewCellMouseEventHandler(this.DataGridViewCellMouseDoubleClick);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息