How to refresh dataGridView table from sql data table afeter delete or editting in c# windows form?

Mohammad 21 Reputation points
2022-07-20T19:35:35.027+00:00

I'm writing a C# windows form application linked with SQL database tables, in widows form. I add a DataGridView to my form and added a table of database to it, when I run the program it show data fine. now I added a delete button to delete the selected sell row's, but when I use this button database table not change. but when I stop program and rerun it again I see that the row was deleted. now I want to know if there is a way to affect this action immediately in database table. this is my code:

private void btnDeleteUser_Click(object sender, EventArgs e)  
        {  
            string Username= dataGridView1[0,dataGridView1.SelectedCells[0].RowIndex].Value.ToString();  
            using (FrmLogin.connection1 = new SqlConnection(FrmLogin.conectionString))  
            using (SqlCommand sqlCommand1 = new SqlCommand("DELETE FROM Users Where UserName='" + Username + "' ", FrmLogin.connection1))  
            {  
                FrmLogin.connection1.Open();  
                sqlCommand1.ExecuteNonQuery();  
                FrmLogin.connection1.Close();  
                MessageBox.Show("Deleted");  
            }  
            dataGridView1.Rows.RemoveAt(dataGridView1.SelectedCells[0].RowIndex);  
        }  

first run image:
223034-1.png

delete a row:
223024-2.png

now I log out without stop and log in the form again. and delete row is in
223046-3.png

now if I stop the program an rerun it deleted row doesn't exist.
223047-4.png

thank you if you can help me about it.

Developer technologies | Windows Forms
Azure SQL Database
Developer technologies | C#
{count} votes

Accepted answer
  1. Jack J Jun 25,296 Reputation points
    2022-07-21T05:25:25.13+00:00

    @Mohammad , Welcome to Microsoft Q&A, you could try to refer to reza's suggestion to use load data to refresh the data.

    Here is a code example you could refer to.

    222917-image.png

    Update Form1 edit button click:

     public int Age { get; set; }  
      
            public string SName { get; set; }  
      
            public int Id { get; set; }  
      
            public DataGridView gridView { get; set; }  
            private void btn_Edit_Click(object sender, EventArgs e)  
            {  
                int index = dataGridView1.CurrentRow.Index;  
                Age = Convert.ToInt32(dataGridView1.Rows[index].Cells["Age"].Value);  
                Id= Convert.ToInt32(dataGridView1.Rows[index].Cells["Id"].Value);  
                SName = dataGridView1.Rows[index].Cells["Name"].Value.ToString();  
                gridView = dataGridView1;  
                Form2 form2 = new Form2();  
                form2.Show();  
      
            }  
    

    Form2:

    223064-image.png

    Tested result:

    223081-2.gif

    Hope the above code could help you.

    Best Regards,
    Jack


    If the answer is the right solution, please click "Accept Answer" and upvote it.If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


1 additional answer

Sort by: Most helpful
  1. Karen Payne MVP 35,586 Reputation points Volunteer Moderator
    2022-07-20T21:45:01.27+00:00

    If you dragged an table to a DataGridView sounds like you are using a TableAdapter which places a DataSet, BindingSource,DataAdapter and TableAdapterManager in the tray below the form.

    If this is the case there are two possibilities

    • The database is part of the project (seen in Solution Explorer) and if so the default for the database under properties is a property Copy to Output Directory where the default is copy always which means on each run in Visual Studio the database is copied to the app folder and wipes out prior changes. If so set Copy to Output Directory to Copy if newer.
    • Unlikely but will mention you are looking at the wrong database.

    Hopefully this fixes your problem

    Now let me say, forget about TableAdapter and consider using a DataAdapter. See simple code sample

    No matter if using a TableAdapter, DataAdapter or Entity Framework, there is never a need to refresh a DataGridView.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.