0

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;
}
kara
  • 3,205
  • 4
  • 20
  • 34
Reza
  • 1
  • show us where `dt` is declared – jazb Jan 24 '19 at 06:04
  • It would be awesome if you could provide a [mcve] – jazb Jan 24 '19 at 06:05
  • What is your question? – ray Jan 24 '19 at 06:08
  • 2
    `dt` appears to be global... the call to `showmedal();` will overwrite the previous call. You need to make two different tables. – JohnG Jan 24 '19 at 06:13
  • Since you share the DataTable both its data and its currencypointer get shared. Insert a BindingSource for at least one DGV! See [here](https://stackoverflow.com/questions/23886653/comboboxes-are-linked-for-some-reason/23894146?r=SearchResults&s=4|28.9803#23894146) - But: If you actually need different data you should use two fifferent DataTables of course. – TaW Jan 24 '19 at 07:51

3 Answers3

0

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;
}
kara
  • 3,205
  • 4
  • 20
  • 34
Shino Lex
  • 487
  • 6
  • 22
0
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();
    }
}
Nirav Vasoya
  • 356
  • 3
  • 18
0

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.

Bart Hofland
  • 3,700
  • 1
  • 13
  • 22