在以前的表单中选择ID号时,以另一种形式显示其他数据库值

时间:2015-08-27 14:49:35

标签: c# winforms

在以前的表格中选择身份证号码时,是否可以向其他表格显示其他详细信息?

这是我之前表单的图片:enter image description here

,这是part of my code,只要点击该行,它就显示在文本框中:

private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0)
            {
                DataGridViewRow row = this.dataGridView1.Rows[e.RowIndex];
                textBox2.Text = row.Cells["id"].Value.ToString();
                textBox3.Text = row.Cells["lastname"].Value.ToString();
                textBox4.Text = row.Cells["firstname"].Value.ToString(); 
            }
        }

我想要的是向另一个表单显示更多细节,如下所示: enter image description here

问题是我不知道如何从中获取语法。你能帮助我,或者至少让我知道如何让第二种形式发挥作用吗?

1 个答案:

答案 0 :(得分:0)

给你一个想法,

//event on which you want to show new form
{
if (e.RowIndex >= 0)
        {
            DataGridViewRow row = this.dataGridView1.Rows[e.RowIndex];
            textBox2.Text = row.Cells["id"].Value.ToString();
            textBox3.Text = row.Cells["lastname"].Value.ToString();
            textBox4.Text = row.Cells["firstname"].Value.ToString(); 
            Form Form2=new Form(row);
            Form2.Show();
            //can use Form2.ShowDialog(); according to requirements
        }
 }

//Constructor of form 2 

public Form2(DataGridViewRow row)
{
    InitializeComponent();
    tbx1.Text = row.Cells[0].Value.ToString();
    tbx2.Text = row.Cells[1].Value.ToString();

}