C# DataGridView binding to dynamic data - c#

I want to create an application that is refreshing DataGridView content with data about surrounding Wifi networks on every second. I wonder what would be the best binding solution for this task. I think using BindingList is meaningless, because it would update the datagridview on every single update on the List, while it would be better to refresh it after whole List is updated. Meaby simple List and rebinding is appropriate here, what do you think?

You can prevent the DataGridView from updating on every change to your BindingList by the following
myBindingList.RaiseListChangedEvents = false;
// Update BindingList
myBindingList.RaiseListChangedEvents = true;
myBindingList.ResetBindings();
The ResetBindings() call will cause your DataGridView to be refreshed to reflect the changes to myBindingList.

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.

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]

Update DataGridView with updated View automatically

I am using View as a Data Source to my DataGridView and I want the DataGridView to update automatically every X time with an updated View, and Its a problem because I am using View instead of SQL command.
As I've searched so far, I need to refresh the data source in the gridview to show updated data and it should be like that:
GridView.DataSource = null;
GridView.DataSource = ViewDataSource;
This does refresh the GridView, but with the same data.
the problem is that the view itself doesn't change, even thought I wrote:
ViewDataSource.EndEdit();
I am pretty sure its because I havn't start editing it and as I know the view was taken as the program started, so I am looking for a way to refresh my view (its my datagridview's data source) with the updated data so I can add it to the grid.
Thanks!
GridView.Refresh() and add Application.DoEvents() for it to refresh immediately.

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.

Binded DataGridView problem

I have a binded DataGridView that allows adding new rows. The problem is that a new object is automatically inserted in the binding source when the CurrentRow is the last grid row.
I want that a new object to be added to the binding source only when the user starts typing in one of the last row cells.
That's not the way DataGridView works, and having tried before to change the way adding new items works, I have to caution you against trying it.
Your best option is to have your binding source implement the ICancelAddNew interface. If this interface is implemented, then the DataGridView will call CancelNew if the user leaves the row without entering any data into the new item. You can also use a BindingList<T> as your data source, or wrap your data source in a BindingSource; both of these classes implement ICancelAddNew.

Categories