3
votes

I set up a Form with three Buttons and two DataGridViews.

When the project is started you have the form with three buttons as mentioned along with a blank DGV1 and a blank DGV2.

First a user presses button 1 triggering the input of a csv file into the DGV1 and displaying - works as intended.

The user presses the second buttton and it saves the contents of DGV1 into a database table.

Press the third button and I bind DGV2 to the database table that just had data added to it. Problem is it only shows the data that was there already and does not include the new data just added. To see the new data I have to exit and then restart.

Here is the Save button code

    private void ButtonSave_Click(object sender, EventArgs e)
    {
        foreach (DataGridViewRow row in DGV1.Rows)
        {
            string constring = @"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\jkitc_000\Desktop\TestProjects\test2\test2\Database1.mdf;Integrated Security=True";
            using (SqlConnection con = new SqlConnection(constring))
            {
                try
                {
                    using (SqlCommand cmd = new SqlCommand("INSERT INTO TeamTable VALUES(@TeamName, @TeamMascot)", con))
                    {
                        cmd.Parameters.AddWithValue("@TeamName", row.Cells["Team"].Value);
                        cmd.Parameters.AddWithValue("@TeamMascot", row.Cells["Mascot"].Value);
                        con.Open();
                        cmd.ExecuteNonQuery();
                        con.Close();
                    }
                }
                catch (Exception ex)
                { }                    
            }
        }
    }

Here is the Show button code

private void button1_Click(object sender, EventArgs e)
{
    DGV2.DataSource = teamTableBindingSource;  
}
1
Probably a duplicate but I have done little with data binding, so not sure stackoverflow.com/questions/7008361/…Eric J.
Try to refresh the DGV2 DGV2.DataSource = teamTableBindingSource; DGV2.Refresh();C1sc0
What is teamTableBindingSource? did you refresh the datasource?User2012384
What is the DataSource of teamTableBindingSource and how do you fill it?Reza Aghaei
tried everything in first suggestion - no help - thanks thoughiamk2

1 Answers

0
votes

Please try this.

After you save your data, call the following function.

DGV1.DataBind();
DGV2.DataBind();

Hope this help...