Updating data in DataGridView while adding records in database

Asked By 55 points N/A Posted on -
qa-featured

Hello! I installed DataGridView1 on the form and then through its property DataSource connect to database. When program starts it shows me records of my database, but then when I add new records they do not appear in program, even after restarting computer. I use DataGridView1.Update() . Please advise what am I doing wrong?

 

SHARE
Answered By 0 points N/A #182598

Updating data in DataGridView while adding records in database

qa-featured
You can use the SqlAdapter for auto update of your DataGrid. See command examples below that you can use appropriately in your program.
 
SqlConnection sql1 = new SqlConnection("Data Source=localhost;Initial Catalog=………. ;Integrated Security=True");
  
SqlDataAdapter sqlda1 = new SqlDataAdapter();
DataTable dtab1 = new DataTable();
 
 
SqlCommand sel1 = new SqlCommand("select ……………….", sql1);
sqlda1.SelectCommand = sel1;
SqlCommandBuilder cmbuilder = new SqlCommandBuilder(sqlda1);
sqlda1.Fill(dtab1);
this.dataGridView1.DataSource = dtab1;
 
 
In the button Save put the following:
 
this.dataGridView1.BindingContext[dtab1].EndCurrentEdit();
this.sqlda1.Update(dtab1);
 
The above commands if used correctly, should update the grid as and when database is updated by program

Related Questions