简体   繁体   中英

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. But when I run the program, both DataGridViews only display the second table information.

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 . Reusing da is no problem, but reusing dt is. When you assign dt to DataGridView.DataSource, the data is NOT copied! So in the end, both DataGridViews will be using the same DataTable object, that holds the data from the second table (medal).

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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