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?
Â
Updating data in DataGridView while adding records in database
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