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

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);
        }

  • 5
    点赞
  • 31
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值