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

时间:2018-11-26 11:37:00

标签: c# winforms datagridview textbox

只有1个dataGridView,我可以根据它们对应的按钮单击(user_tb,project_tb,test_tb)从3个表(一次一个)显示数据库中的不同记录

  • 我希望能够通过单击相应的表记录在相应的文本字段上显示记录。

1)单击show_user btn>用户记录在dataGridView1中显示>单击用户记录> user_id出现在给定的assign_user_txtbx.Text

2)单击show_bug btn>在dataGridView1中显示错误记录>单击错误记录> bug_id出现在给定的assign_bug_txtbx.Text

3)单击show_test btn>测试记录在dataGridView1中显示>单击测试记录> test_id出现在给定的assign_test_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
    }

问题是Im仅使用1个dataGridView

1 个答案:

答案 0 :(得分:0)

我不确定您在问什么。我想您需要使用所选行中的值填充文本框,但是由于您在运行时更改了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 .. else部分中使用该标志