DataGridView Bound to DataTable ResetBinding(False) Sets Grid Position to First Row

welburn1010
welburn1010 used Ask the Experts™
on
Hi

I have a DataGridView which i update using a DataTable and a BindingSource.

I need to hide the the updating of the data so i use bindingSource.RaiseListChangedEvents = False
then add the new data to the datatable then use bindingSource.RaiseListChangedEvents = True.

Then to make the control update the data i use bindingSource.ResetBinding(False).

The problem i have is when i call ResetBinding(False) the scroll position of the gridview goes to to first row in the grid, but i need it to maintain the exact same position it was in before the update.

Any Ideas?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Before you update your datagridview assign your datagridview's FirstDisplayedScrollingRowIndex property to an integer variable;

int vso = dataGridView1.FirstDisplayedScrollingRowIndex;

Perform your update and assign the variable back to the datagridview's same property as its shown below:

                if (vso != -1)
                    dataGridView1.FirstDisplayedScrollingRowIndex = vso;

Hope it hekps..
x77

Commented:
The DataGridView Supports three modes:
  1. Data add to Rows
  2. DataBinding
  3. VirtualMode
if we use DataBinding, must accept the model or a lot of code required to accomplish the deviation. A lot of Test required to identify unexpected bugs.
The First Mode is the more easy and flexible when binding is not affordable.
I like DataBind Mode, but...
Then If we need  update DataTable, use events of DatagridView.
  • RowValidated
  • UserDeletingRow
I Use Tag of DataGridViewRow to identify the related DataRow.
On RowValiated, If Tag is nothing, then DataTable NewRow...
By sample, I need to order the data by a calculated (depends on external data) column. This is no affordable by DataView.
Use of VirtualMode is more complex.





 'Add Rows To No Binding DataGridView
  Dim rrcd = DGVTrCd.Rows, rrci = DgvTrCi.Rows, i As Integer
  For Each r As DataRow In TrCondic.Rows
      If r(cCodTra) = Key Then
         i = rrcd.Add(New Object() {r(cTCond), r(cCond), _
             r(cdemora), Fimte(r)})
             rrcd(i).Tag = r
      ElseIf r(cCond) = Key Then
         i = rrci.Add(New Object() {r(cCodTra), r(cTCond), _
             r(cdemora), Ffmta(r)})
             rrci(i).Tag = r
      End If
  Next
  DGVTrCd.Sort(CondFimte, ListSortDirection.Descending)
  DgvTrCi.Sort(CondFfmta, ListSortDirection.Ascending)

Open in new window

Author

Commented:
Not answered with a possible use
Uwe

Commented:
Assume a table containing bills and a table containing the positions for that bill.
Those tables are "joined" with a relation/constrained.
For all tables, corresponding BindingSources exist.

The BindingSource of the positions is bound to a DataGridView.
The BindingSource of the bill is bound to several other Controls (e.g. Text-Controls or labels).

One control contains the total amount and should update when a position was changed.

That´s done in the BindingSource-CurrentItemChanged-Event (of the Positions).
In that event-method, the sum gets calculated and update in the DataRow of the bill.

Unfortunately, this triggers all possible events for all related tables/bindingsources.

As a result, the selected cell in the DataGridView of the positions was
changed as soon as any data in a position was modified (also when adding a new row).

I managed to workaround this issue by doing the following when updating the total sum of a bill:

Decimal d_bill_total = 0; // To be calculated 
DataRow dr_Bill = ((DataRowView)m_BindingSource_Bill.Current).Row;

m_BindingSource_Bill.RaiseListChangedEvents = false;
dr_Bill["bill_total"] = d_bill_total;
foreach (Binding b_enum in m_BindingSource_Bill.CurrencyManager.Bindings)
{
    b_enum.ReadValue();
}
m_BindingSource_Bill.RaiseListChangedEvents = true; 

Open in new window


I hope this will help someone sometime...
Best regards, Uwe

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial