Update Datatable and DatagridView with database Changes by Timer

Posted by aleroot on Stack Overflow See other posts from Stack Overflow or by aleroot
Published on 2010-05-01T09:09:36Z Indexed on 2010/05/01 10:47 UTC
Read the original article Hit count: 229

Filed under:

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 ?

© Stack Overflow or respective owner

Related posts about c#