简体   繁体   English

如何以一种形式(C#)使用2个DataGridViews?

[英]how to use 2 DataGridViews in one form (c#)?

I want to use two DataGridViews in this form, which will receive their information from two different tables in one database. 我想以这种形式使用两个DataGridView,它们将从一个数据库中的两个不同表中接收它们的信息。 But when I run the program, both DataGridViews only display the second table information. 但是,当我运行该程序时,两个DataGridViews仅显示第二个表信息。

private void Ring_Load(object sender, EventArgs e)
{
    showdata();
    showmedal();  
}

void showdata()
{
    SqlConnection con = new SqlConnection(conn);
    SqlCommand cmd = new SqlCommand("SELECT Number,Weight,Ring_Id FROM Ring", con);
    da.SelectCommand = cmd;
    dt.Clear();
    da.Fill(dt);
    dataGridView1.DataSource = dt;
    dataGridView1.Columns[3].Visible = false;

}

void showmedal()
{
    SqlConnection con = new SqlConnection(conn);
    SqlCommand cmd = new SqlCommand("SELECT Number,Weight,Ring_Id FROM medal", con);
    da.SelectCommand = cmd;
    dt.Clear();
    da.Fill(dt);
    dataGridView2.DataSource = dt;
}

Try this 尝试这个

private void Ring_Load(object sender, EventArgs e)
{
    showdata();
    showmedal();  
}

void showdata()
{
    SqlConnection con = new SqlConnection(conn);
    SqlCommand cmd = new SqlCommand("SELECT Number,Weight,Ring_Id FROM Ring", con);
    da.SelectCommand = cmd;
    dt.Clear();
    da.Fill(dt);
    dataGridView1.DataSource = dt;
    dataGridView1.Columns[3].Visible = false;

}

void showmedal()
{
    SqlConnection con = new SqlConnection(conn);
    SqlCommand cmd = new SqlCommand("SELECT Number,Weight,Ring_Id FROM medal", con);
    da.SelectCommand = cmd;
    dt.Clear();
    dt = new DataTable();
    da.Fill(dt);
    dataGridView2.DataSource = dt;
}
private void Ring_Load(object sender, EventArgs e)
{
    showdata();
    showmedal();  
}

void showdata()
{
    SqlConnection con = new SqlConnection(conn);
    SqlCommand cmd = new SqlCommand("SELECT Number,Weight,Ring_Id FROM Ring", con);
    da.SelectCommand = cmd;
    using(DataTable dt = new DataTable())
    {
        da.Fill(dt);
        dataGridView1.DataSource = dt;
        dataGridView1.DataBind();
        dataGridView1.Columns[3].Visible = false;
    }
}
void showmedal()
{
    SqlConnection con = new SqlConnection(conn);
    SqlCommand cmd = new SqlCommand("SELECT Number,Weight,Ring_Id FROM medal", con);
    da.SelectCommand = cmd;
    using(DataTable dt = new DataTable())
    {
        da.Fill(dt);
        dataGridView2.DataSource = dt;
        dataGridView2.DataBind();
    }
}

You seem to be reusing da and dt . 您似乎正在重用dadt Reusing da is no problem, but reusing dt is. 重用da是没有问题的,但重用dt是没有问题的。 When you assign dt to DataGridView.DataSource, the data is NOT copied! dt分配给DataGridView.DataSource时,不会复制数据! So in the end, both DataGridViews will be using the same DataTable object, that holds the data from the second table (medal). 因此,最后,两个DataGridViews将使用相同的DataTable对象,该对象保存第二个表(勋章)中的数据。

You could try this: 您可以尝试以下方法:

private void Ring_Load(object sender, EventArgs e)
{
    showdata();
    showmedal();  
}

void showdata()
{
    SqlConnection con = new SqlConnection(conn);
    SqlCommand cmd = new SqlCommand("SELECT Number,Weight,Ring_Id FROM Ring", con);
    da.SelectCommand = cmd;
    DataTable dt1 = new DataTable();
    da.Fill(dt1);
    dataGridView1.DataSource = dt1;
    dataGridView1.Columns[3].Visible = false;

}
void showmedal()
{
    SqlConnection con = new SqlConnection(conn);
    SqlCommand cmd = new SqlCommand("SELECT Number,Weight,Ring_Id FROM medal", con);
    da.SelectCommand = cmd;
    DataTable dt2 = new DataTable();
    da.Fill(dt2);
    dataGridView2.DataSource = dt2;
}

Edit: renamed the local DataTable variables for clarity. 编辑:为清楚起见,重命名了本地DataTable变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM