user2160781
user2160781

Reputation: 67

Show data from Database in Datagridview

I have a problem showing data from database in my DataGridView (dataGridView1). I have a button (btnInsert) which saves input data to database, but to show the data in the datagridview I have to restart the application. What should I do ?

Upvotes: 0

Views: 474

Answers (2)

user2230191
user2230191

Reputation: 21

After your event handler is fired, you need to set your datagrid's datasource property with a datatable that contains data from a query. Once this is done, bind your datagrid like so:

dataGrid1.DataSource = new BindingSource(dt, null);

Upvotes: 0

Kasnady
Kasnady

Reputation: 2279

At your save button, rebind again the startup code.

 BindingSource binding = new BindingSource(); //req. by win forms
 DataTable dt = new DataTable();
 dt.Load(sql_command.ExecuteReader());

this.ticket_1 TableAdapter.Fill(this.ticketDataSet2.Ticket_1); dgv.DataSource = dt;

This is the best way I've found to do it in win forms, .update doesn't work because it needs to actually re-pull the data from SQL. or you can try to check this link

Upvotes: 1

Related Questions