Sorting datagrid-dataview-datatable

G

Guest

datagrid is bound to dataview.
the rows will sort when clicking on datagrid column headers..

however, selecting a row after sort reveals that the datatable still maintains original order. rows were not sorted.
how do i ensure underlying datatable gets sorted with datagrid sort

thank
 
V

Val Mazur

Hi,

DataGrid is just a part of the user interface and it does not affect actual
data in a datatable in a case of sorting. Actually I do not see any reason
for it. If you need to reference to the specific row in a datatable, then
you would need to use some sort of the unique columns (like primary key),
not the index of the row
 
W

William Ryan eMVP

Like Val mentioned, the grid is simply a UI aid for the bound object.
Whatever you do to it is done to the underlying table. Notice that
DataTables don't have a .Sort method. A DataView does. The table has a
defultView property that can be sorted. However, if you create a datatview
on a Datatable or use DefaultView and change the sort order (say by default
it was a column name CustID) to LastName if DataTable.Rows[0][0] before
equalled 1 ie CustID = 1,
then DataTable.Rows[0][0] will still be one after the sort.

However, the DataView will have a different sort order so if anything,
that's what you may want to go after. The bottom line is that Tables don't
sort per se, you can change their positions through refilling them with a
new select statement specifying a new sort order or some other (painful)
mechanism, but the order doesn't change. You can bind to a DataView instead
and manipulate the sort all you want. Then, when you change a vlaue in the
dataview (through code or through the grid) the change will pass through to
the underlying datatable.

HTH,

Bill

--

W.G. Ryan, eMVP

http://forums.devbuzz.com/
http://www.knowdotnet.com/williamryan.html
http://www.msmvps.com/WilliamRyan/
http://www.devbuzz.com/content/zinc_personal_media_center_pg1.asp
subT said:
datagrid is bound to dataview..
the rows will sort when clicking on datagrid column headers...

however, selecting a row after sort reveals that the datatable still
maintains original order. rows were not sorted.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top