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;
}
DGV2.DataSource = teamTableBindingSource; DGV2.Refresh();
– C1sc0DataSource
ofteamTableBindingSource
and how do you fill it? – Reza Aghaei