Update Datatable and DatagridView with database Changes by Timer
- by aleroot
Scenario : i have a database table that is being updated frequently by some services.
I have a c# Winforms Application that load this table in a datagridview by binding a datatable as Datasource, then i whant to add a Timer that every 10 seconds update a the content of a datatable with the last changes in the database table ...
I don't need to update a database with the datatable changes, but i need to update datatable with the last changes in the database table, that is the inverse of the usually....
Is there a way to do that ? What is the best way ?
i've tried with this code :
private void ServiceTimer_Tick(object state)
{
OdbcConnection oCon = new OdbcConnection();
oCon.ConnectionString = ConnectionStrings;
OdbcDataAdapter dp = new OdbcDataAdapter("SELECT * FROM table", oCon);
dsProva.Tables.Clear();
dp.Fill(dsProva,"table");
dataGridViewMessaggi.DataSource = dsProva.Tables["table"];
dataGridViewMessaggi.Refresh();
}
But every Timer Tick i lost the selection in DatagridView and Current Row ....
Is There a better solution ?