双击dataGridView单元格显示详细信息

  //Form1

 public DataGridViewRow dataGridViewRow;//在修改中用到该变量,用来传送要修改的记录信息

 private void dataGridView1_DoubleClick(object sender, EventArgs e)
        {
            //得到当前行
            dataGridViewRow = dataGridView1.CurrentRow;
            if (dataGridViewRow != null)
            {
                //修改当前信息
                RenYuan ren = new RenYuan();
                ren.ShowDialog(this);
            }
          
        }

        private void button1_Click(object sender, EventArgs e)
        {
            using(SqlConnection con=new SqlConnection("server=.;uid=sa;pwd=sa;database=xiaoxiDemo"))
            {
                SqlDataAdapter da = new SqlDataAdapter("select * from xi",con);
                DataTable dt = new DataTable();
                da.Fill(dt);
                this.dataGridView1.DataSource = dt;
                con.Close();
            }
          
        }
    }

//Form2

 private void RenYuan_Load(object sender, EventArgs e)
        {
            this.txtID.Text = ((Form1)(this.Owner)).dataGridViewRow.Cells[0].Value.ToString();
            this.txtSex.Text = ((Form1)(this.Owner)).dataGridViewRow.Cells[1].Value.ToString();
            this.txtAge.Text = ((Form1)(this.Owner)).dataGridViewRow.Cells[2].Value.ToString();
        }

原文地址:https://www.cnblogs.com/freexiaoyu/p/1298562.html