Sort Datagrid without Dataview

M

msxkim

Is there a way to sort a datagrid without using Dataview?
I would like to sort it by 3rd column but programmatically.
 
V

VR

Actually I do not know a way to perform this.

The primary goal of the datagrid control is to setup a user friendly view of
data (and a easy way to handle updates/delete/etc), that is, the data is not
fisically in the grid, it's in the dataTable and the grid just has the
ability to show it formatted on screen, so, all you can do is use the
dataView object.

The easiest way to deal with this is using the .DefaultView() property of
the dataTable to bind it to the grid, that is:

DataGrid.DataSource = DataTable.DefaultView()

and there on just use methods in code like:

DataTable.DefaultView.Sort = "Customer"

With this, the data in the grid is updated automatically to reflect the
current .DefaultView() state.

PS: if you want to deal with data directly in code (that is, not using the
DataGrid features) you should perform all operations directly on the
datatable (like, if you delete a row from the table, the grid is
automatically updated) but do not forget: if you're using the .DefaultView()
as the dataGrid's dataSource property, ALWAYS use the
DataTable.DefaultView() to perform all operations to avoid sync issues, that
is, depending on the "sort" you use, the DataTable.Rows(0) can be differente
that DataTable.DefaultView(0).

regards,
Victor Reboucas
 

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

Similar Threads


Top