update datagridview when database change - c#

I have 2 computer: server and client,same winform app on both,same database.
I want to be able to update the datagridview on database change
So I made a ticker update every 4 seconds that refresh the datagridview datasource.
Few problems when the datasource is changing
First if a row was choose(dataGridView_RowHeaderMouseClick) inside the datagrid ,it lost focus.
seconed if I scroll down the datagridview,the scroll bar jump to the start.
Any Idea on how to do it right?
Thanks
Baaroz

You do not want to refresh the DataSource every 4 seconds without any conditions. As you say, there are several functionality issues that will be affected by this (such as losing focus on a row, that although could be solved by storing the row handle every time you focus into a row, shouldn't be something that you need to do), without mentioning the fact that if the tables you are loading are large there will be a performance issue caused by the constant reload.
You should either trigger a refresh every time the DataSource changes, or what Anthbs says, compare the data with your grid's DataSource and only refreshing if they are different.

Heres a couple of idea's to reduce the refresh issues.
To reduce the chance of this occuring you could do a comparison of the new data with the data in the grid only refresh if the data has changed.
You could store the selected index before you refresh the grid and set it back after you have refreshed.

Related

DataGridView Refresh - How to not re-write the whole grid?

After adding a row to a DataGrid, I would like to update my DataGridView (with the input source of said DataGrid) using that new row. However when I use .Update() and .Refresh(), the whole of the grid gets re-drawn. When refreshing relatively quickly (around 4 times per second), this creates an unpleasing jolty effect. I would like to find a way to add my row to the DataGridView, without re-drawing the whole thing, therefore removing the DataGridView.
C# Winforms
I think what you are looking for is a data binding.
https://learn.microsoft.com/en-us/dotnet/desktop/winforms/controls/how-to-bind-data-to-the-windows-forms-datagridview-control?view=netframeworkdesktop-4.8
This means the DataGrid is bound to e.g. a DataTable.

ResetBindings for just one column on a datagridview

I am working with windows forms using C#.
I currently have an object bound to a DataGridView.
One of the properties of the object is Currentprice
This is updated every few seconds from an API.
In order for this change to be reflected in the DataGridView, I am calling
MyObject.ResetBindings();
every time its updated.
This works Fine, but there is a noticeable flicker every time this happens.
Is there any way to refresh only the CurrentPrice column in the datagridview?

update DataGridView without losing selection

I'm trying to update data I have in a datagridview (based on checks set by the user), without losing the selection (on which a calculation is made). This works for me, but is a lot slower than clearing the complete dgv, making a new datatable and binding it.
This is what I tried :
when the datagridview need to be completely updated (different amount of rows), I clear the datagridview, make a new datatable and bind it. This works nice and fast, but the selection is lost. It's no problem the selection is lost, because the data is completely new.
sometimes I only need to update the values in the datagridview (and I don't want to lose the selection). I tried to either update the values in the bound datatable (works) or the values in the datagridview directly (this also works). However, both are a lot slower than just making a whole new datagridview using the first method :-(
when updating the datagridview, I stop drawing the panel on which it is placed
all events that the datagridview fires are suspended
autoresize etc is off
Can anyone explain why updating the cellvalues is so much slower than redoing the whole thing?
Is there another way to do it without losing the selection? I could remember the selection and set it again, but then I lose the way/order cells and columns where selected.
I finally found where the big delay occured. I was completely focused on the DataGridView, but it turned out the delay happend in the DataTable that was bound to it.
In order to do it fast :
first set each row of the DataTable to BeginEdit, so changes are not acted upon. After updating the DataGridView, do not make your DataTable accept the changes, it will make you lose the selection in the DataGridView.
[code]
for (int Row = 0; Row < MyDataTable.Rows.Count; Row++) MyDataTable.Rows[Row].BeginEdit();
// do your changes in the DataGridView (not the DataTable) here
UpdateDataGridView();
// never accept the changes in the DataTable, it will lose the selection in the DataGridView. Problem is that the DataTable is never updated, but this is not a problem in my case.
//MyDataTable.AcceptChanges();
[/code]

c# Scroll bars keep resetting when DataGridView updates

I am binding my grid as such:
dataGridView1.DataSource = new BindingSource();
dataGridView1.DataSource = tableData; (tableData is an ArrayList of custom objects)
The dataSource is getting updated very often (the file it is reading from gets updated about every 2 ms). So when I am scrolling, the scrollbar will jump to it's original position upon a refresh. I refresh like this:
((CurrencyManager)dataGridView1.BindingContext[tableData]).Refresh(); (this happens once every ~1 second)
How can I scroll without the scroll bar resetting every time the datagridview gets refreshed?
Take a look at this. Although the question is about Winforms DataGrid, the answer is applies to DataGridView. You need to store FirstDisplayedScrollingRowIndex before the reload and restore it after.
I know it's been a while since you posted this question but I just ran into this kind of issue myself. One thing to check is to be sure you are not setting the CurrentCell property when your grid updates
Code such as the following will cause your scroll position to reset. You are telling it to view a particular row, and the first cell in that row.
YourGrid.CurrentCell = YourGrid[0, row];
Hope this helps.
DC

How do I smoothly update a GridView by updating the bound DataTable in C#?

I have a GridView who's DataSource is set to a DataTable. The DataTable is updated by some backend logic every few seconds, at which point a delegate is called to refresh GridView.
Currently I am simply resetting the DataSource, but that causes a problem - it interrupts any ongoing edits in the grid view and makes the selection 'jump' to the top-left cell.
The update logic basically creates a new (identical with regard to columns and rows) DataTable.
Is there any standard way to do it without any drawbacks? Is my only option updating the current DataSource row by row, inserting values programmatically?
Thanks!
You should use a BindingList or some data source that supports change notification.
I'm confused by many things in this question. If you're using a GridView, and not a DataGridView, then you're either using ASP.NET, WPF, or .NET 1.1. Which is it?
Next: you're creating a new DataTable entirely? Well of course the control's going to get reset when you reset the DataSource. It doesn't know that the schema of your new DataTable is the same as the one it's replacing. It's got to go through the columns and re-establish the bindings.
Also, of course it's losing the current row. The current row belongs to the old DataTable, not the new one.
If you want a bound control to retain its state when you update the underlying data source, update the underlying data source, don't replace it with a new one.
Do your updates happen on a background thread? I don't know if it will work in your scenario, but you could try this threaded binding list; see the example to see the worker merrily editing the grid.

Categories