3

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;  
}
Grant Winney
  • 65,241
  • 13
  • 115
  • 165
iamk2
  • 31
  • 2
  • Probably a duplicate but I have done little with data binding, so not sure http://stackoverflow.com/questions/7008361/how-can-i-refresh-c-sharp-datagridview-after-update – Eric J. Oct 09 '15 at 01:21
  • Try to refresh the DGV2 `DGV2.DataSource = teamTableBindingSource; DGV2.Refresh();` – IamK Oct 09 '15 at 01:21
  • What is teamTableBindingSource? did you refresh the datasource? – User2012384 Oct 09 '15 at 01:22
  • What is the `DataSource` of `teamTableBindingSource` and how do you fill it? – Reza Aghaei Oct 09 '15 at 02:00
  • tried everything in first suggestion - no help - thanks though – iamk2 Oct 09 '15 at 02:05
  • catch keeps program going instead of throwing error that there are no more rows to load ------ adding DGV2.Refresh(); does not help either -------- teamTableBindingSource is the database table binding to DGV2. – iamk2 Oct 09 '15 at 02:13
  • Database table is bound to Datagridview - new data is added to database table - do you have to do some sort of refresh for the table to get the refresh of the grid to include the new data – iamk2 Oct 09 '15 at 02:15
  • "Database table is bound to Datagridview" doesn't make sense, you bind a grid to a binding source and bind a binding source to something like datatable or assign a list to datasource of bindingsource, and you should reload data to that data table or that list – Reza Aghaei Oct 09 '15 at 02:18
  • How about I say it like this - The database table is connected to the datagridview using the teamTableBindingSource. Does that sound correct? – iamk2 Oct 09 '15 at 02:48
  • It's better to share the code that you performed that binding. – Reza Aghaei Oct 09 '15 at 02:51
  • I did not have to code the binding myself. It is in VS 2015. I added a DGV then selected datagridview task where I was able to choose a data source for it. The name teamTableBindingSource is the result – iamk2 Oct 09 '15 at 02:56

1 Answers1

0

Please try this.

After you save your data, call the following function.

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

Hope this help...

Wen21
  • 93
  • 8
  • databind is not a definition of datagridview – iamk2 Oct 09 '15 at 03:00
  • Sorry, didn't notice this is datagrid... try this in your Page_Load: BindingSource bindingSource1 = new BindingSource(); DGV2.DataSource = bindingSource1; GetData("select * from TeamTable"); – Wen21 Oct 09 '15 at 03:38