views:

61

answers:

1

I am sorting datatable datewise like

TableWithOnlyFixedColumns.DefaultView.Sort = "TradingDate asc";

then assign this datatable to datagridview to display the sorted records like.

datagridView1.DataSource =TableWithOnlyFixedColumns.DefaultView;

But the problem is when datatable is updated means is changed then according to datatable, datagridview also updates its records but I want like when above statement execute again it should update its record. And if I copy the recrods from the datatable to datagridview cell by cell manually then records in the datagridview is not sorted datewise.

What I can do for this ?

+3  A: 

A DataGridView, when given a DataSource is inherently data-bound. You can suspend notifications (for example, by going via a BindingSource and setting RaiseListChangedEvents to false), but this is just notifications - it is still looking at the same IListSource / IList etc.

To get truly isolated data, either:

  • don't data-bind (set the cells manually), or
  • take a snapshot / clone of the data
Marc Gravell
@Marc Gravell,If I set the cells manually it does not give the output in sorted form because I have sorted the records datewise and if it is possible for me by other approach then it will be best for me.Or How can we clone the data of the datatable to datagridiew ?
Harikrishna
@Marc Gravell,If I set the cell manually there is no sorted records which I have posted on question : http://stackoverflow.com/questions/2764879/datagridview-and-sorted-records-of-datatable
Harikrishna
@Harikrishna - sorting is tricky, via either of the two approaches that `DataGridView` supports. `IBindingList[View]` is a pain to implement. Suggest you just clone the `DataTable`...
Marc Gravell
@Marc Gravell,How to clone the datatable ?
Harikrishna
@Harikrishna - a `DataTable` instance has a `Clone()` method; set `myGrid.DataSource = myDataTable.Clone();`
Marc Gravell
@Marc Gravell,If I do like `myGrid.DataSource=myDataTable.Clone();`I can not find any rows in datagridview.
Harikrishna
@Harikrishna : My mistake; `Clone` only does structure; to copy the data too: `DataTable newTable = oldTable.Clone(); foreach (DataRow oldRow in oldTable.Rows) { newTable.ImportRow(oldRow); }`
Marc Gravell
@Marc Gravell,With doing this new datatable does not contain sorted records But I want sorted records not defualt records.
Harikrishna
@Harikrishna - tables aren't sorted; views are sorted. Copy the sort of the default view: `newTable.DefaultView.Sort = oldTable.DefaultView.Sort;`
Marc Gravell
@Marc Gravell,Thanks for the great answer.
Harikrishna