Is there BeginUpdate() functionality for the DataGridView?

I have a DataGridView bound to a DataView. A certain points I temporarily sort the DataGridView on specific columns to build filter lists. During these "temporary" sorts on the DataView I would like stop the sort from being applied on the DataGridView, which causes it to redraw. Then after the filter list is created I reapply the DataGridView's last sort.
ComboBoxes have BeginUpdate() and EndUpdate() for when changes are made to the bound list. Is there something along the same lines for the DataGridView

Thanks,
Nate


Answer this question

Is there BeginUpdate() functionality for the DataGridView?

  • Pablo Espada

    Hmm... Can you post here code that doing this temporary sorts

  • jockofett

    Yeah, I've also tried it with a BindingSource object sitting between the DataGridView and DataTable and SuspendBinding() did not work.

  • skl5973

    You use BindingSource Did SuspendBinding() works

  • Edmundas

    But why you need such sort For internal data processing Then you can create another DataView and sort there, because it will not be connected with DGV - nothing will happens on screen in intermediate sorts.

  • beechy34

    Tested that. It doesn't work. The DataGridView is a Complex binding so SuspendBinding will not prevent it from getting changes made to its datasource.

  • Daniel.Alvarez

    Filters like in Excel when you select AutoFilter I see. You should really use another DataView or use Select on the DataTable (I think Select is more appropriate).

    DataView keeps filtered and sorted rows from DataTable, but each row is a reference. Overhead is not too much, for 100 000 items your app will temporarily need about 400KB memory, I think it's acceptable in most cases.



  • Christopher Lukas

    Well, you can actually turn off DataSource in DataGridView before sort and rebind to it after sort.

    Perhabs you need this. If you want to sort with multiple columns, then simple write bs.Sort="ColumnOne DESC,ColumnTwo DESC". So all sorting done in single call.



  • Fleurin

    Sorting is applied to the DataView object (or BindingSource) and not on the DataTable. So using such methods on the source DataTable will have no affect. I tested it just to make sure and it didn't work.

  • HerbF

    What about DataTable.BeginLoadData()/EndLoadData() They also claims to prevent notifications.

  • Jeff Filek

    Like I noted in the first post I'm creating Filter lists to be loaded in to ComboBoxes. Users can then use the filter lists to narrow down results in the DataGridView. I use temporary sorts to clump together common fields then I make distinct lists by looping through the records selecting each distinct item to be part of the filter list.
    I guess I could keep a seperate DataView around and apply the sort there. It would be a valid option, but I wonder how much overhead there is in keeping another DataView in memory for very large lists.

  • Jesse990

    Below is my test using the BindingSource and SuspendBinding():

    Public Class Form1

    Private bs As BindingSource
    Private dt As DataTable

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    dt = New DataTable
    dt.Columns.Add("ColumnOne", GetType(String))
    dt.Columns.Add("ColumnTwo", GetType(String))

    dt.Rows.Add(New String() {"One", "Nine"})
    dt.Rows.Add(New String() {"Two", "Eight"})
    dt.Rows.Add(New String() {"Three", "Seven"})
    dt.Rows.Add(New String() {"Four", "Six"})
    dt.Rows.Add(New String() {"Five", "Five"})
    dt.Rows.Add(New String() {"Six", "Four"})
    dt.Rows.Add(New String() {"Seven", "Three"})
    dt.Rows.Add(New String() {"Eight", "Two"})
    dt.Rows.Add(New String() {"Nine", "One"})

    bs = New BindingSource(dt, "")
    dgv1.DataSource = bs
    '-- default sort of ColumnOne DESC
    bs.Sort = "ColumnOne DESC"
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    bs.SuspendBinding()
    '-- I want these sorts to be applied to the BindingSource but not redrawn in the DataGridView.
    bs.Sort = "ColumnTwo DESC"
    MessageBox.Show("Note that the sort has been applied to the DataGridView.")
    bs.Sort = "ColumnOne DESC"
    bs.ResumeBinding()
    End Sub
    End Class


  • John Valente - MSFT

    I'm not worried about multiple column sorting here, and removing the datasource is not ideal because the list will go blank momentarily.
  • polachan

    DataTable.Select does not allow you to use Distinct. If you check MSDN for DataColumn.Expression it shows what is available for filtering. Distinct is not implemented.

    I did find that DataView.ToTable allows me to specify distinct results, and I can specify a subset of columns for output. This should work for making distinct lists. Except, I still have the sorting problem. It would have been a great feature to allow ToTable to sort the data being returned.

  • GamePgmr

    Hi!

    You can get BindingManagerBase from BindingContext and call SuspendBinding()/ResumeBinding().



  • Is there BeginUpdate() functionality for the DataGridView?