Click here to Skip to main content
15,903,362 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Scenario : i have a database table that is being apdated frequently bey some services.

I have a c# Winform 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 ?
Posted

1 solution

This is happening becoz you are rebinding your datagrid on every timer tick.
As per your need, this code is ok, i don't know any better solution for this.
But what can you do is store your currently selected row in a static variable before rebinding datasource.
After rebinding, check whether that row exist in datagrid or not, if yes then set that row as selected. :)

There might be a better solution for this which someone know, till that time you can try this.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900