C# 如何仅使用一个dataGridView在不同表的文本字段上显示不同的记录?

C# 如何仅使用一个dataGridView在不同表的文本字段上显示不同的记录?,c#,winforms,datagridview,textbox,C#,Winforms,Datagridview,Textbox,由于只有一个dataGridView,我可以根据相应的按钮单击(用户\ tb、项目\ tb、测试\ tb)在数据库中显示来自3个表的不同记录(一次一个) 我希望能够通过单击相应的表记录,在相应的文本字段中显示记录 1) 单击显示用户btn>在dataGridView1中显示用户记录>单击用户记录>用户id显示在给定的assign\u user\u txtbx.Text private void multi_dataGridView1_CellContentClick(object sende

由于只有一个dataGridView,我可以根据相应的按钮单击(用户\ tb、项目\ tb、测试\ tb)在数据库中显示来自3个表的不同记录(一次一个)

  • 我希望能够通过单击相应的表记录,在相应的文本字段中显示记录
1) 单击显示用户btn>在dataGridView1中显示用户记录>单击用户记录>用户id显示在给定的
assign\u user\u txtbx.Text

private void multi_dataGridView1_CellContentClick(object sender,   DataGridViewCellEventArgs e)
    {
        if (e.RowIndex >= 0)
        {
            DataGridViewRow Row = this.multi_dataGridView1.Rows[e.RowIndex];

            assign_user_txtbx.Text = Row.Cells["user_id"].Value.ToString();

        } // end of if statement

        if (e.RowIndex >= 0)
        {
            DataGridViewRow Row = this.multi_dataGridView1.Rows[e.RowIndex];

            assign_project_txtbx.Text = Row.Cells["project_id"].Value.ToString();

        } // end of if statement

        if (e.RowIndex >= 0)
        {
            DataGridViewRow Row = this.multi_dataGridView1.Rows[e.RowIndex];

            assign_test_txtbx.Text = Row.Cells["test_id"].Value.ToString();

        } // end of if statement
    }
2) 单击show_bug btn>bug records show in dataGridView1>单击bug records>bug id出现在给定的
assign_bug\u txtbx.Text

private void multi_dataGridView1_CellContentClick(object sender,   DataGridViewCellEventArgs e)
    {
        if (e.RowIndex >= 0)
        {
            DataGridViewRow Row = this.multi_dataGridView1.Rows[e.RowIndex];

            assign_user_txtbx.Text = Row.Cells["user_id"].Value.ToString();

        } // end of if statement

        if (e.RowIndex >= 0)
        {
            DataGridViewRow Row = this.multi_dataGridView1.Rows[e.RowIndex];

            assign_project_txtbx.Text = Row.Cells["project_id"].Value.ToString();

        } // end of if statement

        if (e.RowIndex >= 0)
        {
            DataGridViewRow Row = this.multi_dataGridView1.Rows[e.RowIndex];

            assign_test_txtbx.Text = Row.Cells["test_id"].Value.ToString();

        } // end of if statement
    }
3) 单击显示测试btn>测试记录在dataGridView1中显示>单击测试记录>测试id显示在给定的
assign\u test\u txtbx.Text

private void multi_dataGridView1_CellContentClick(object sender,   DataGridViewCellEventArgs e)
    {
        if (e.RowIndex >= 0)
        {
            DataGridViewRow Row = this.multi_dataGridView1.Rows[e.RowIndex];

            assign_user_txtbx.Text = Row.Cells["user_id"].Value.ToString();

        } // end of if statement

        if (e.RowIndex >= 0)
        {
            DataGridViewRow Row = this.multi_dataGridView1.Rows[e.RowIndex];

            assign_project_txtbx.Text = Row.Cells["project_id"].Value.ToString();

        } // end of if statement

        if (e.RowIndex >= 0)
        {
            DataGridViewRow Row = this.multi_dataGridView1.Rows[e.RowIndex];

            assign_test_txtbx.Text = Row.Cells["test_id"].Value.ToString();

        } // end of if statement
    }

问题是我只使用了1个dataGridView,我不知道你们在问什么。我认为您需要用所选行中的值填充文本框,但由于您在运行时更改了DataGridView的内容,因此需要获取的值也可能不同。
如果是这样的话,也许这样的事情可以让你开始

private void multi_dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
    if (e.RowIndex >= 0)
    {
        DataGridViewRow Row = this.multi_dataGridView1.Rows[e.RowIndex];

        if (Row.Cells["user_id"] != null)
            assign_user_txtbx.Text = Row.Cells["user_id"].Value.ToString();
        else if (Row.Cells["project_id"] != null)
            assign_project_txtbx.Text = Row.Cells["project_id"].Value.ToString();
        else if (Row.Cells["test_id"] != null)
            assign_test_txtbx.Text = Row.Cells["test_id"].Value.ToString();
    } // end of if statement
}

另一种方法是在单击更改datagridview视图的按钮时设置某种标志,并在
if。。否则
part

单击按钮时更改DataGridView的数据源应该可以这样做您可以解释更多详细信息请,我可以在DataGridView上显示数据库很好…我单击按钮的唯一时间是在DataGridView上显示我的数据库…如果我理解您的问题正确,单击按钮后,您希望在DataGridView中显示不同的数据。因此,在单击中,将DataGridView的数据源更改为另一个resultset。如果这不是你的问题,请在你的问题中更好地解释你需要什么以及你的确切问题是什么。目前你的问题很模糊,我已经可以在DataGridView中显示不同的数据,我想要的是当我从DataGridView中单击/选择一条记录时,我希望特定的数据显示在相应的文本字段中。问题是,因为我已经在DataGridView上显示了一个数据库,所以当我选择一个记录时,它会给我一个错误“assign_project_txtbx.Text=Row.Cells[“project_id”].Value.ToString();数据不存在,因为我需要从另一个数据库检索该数据。