Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

I have created a view using the DefaultView.Sort property. If I bind this data to a data grid I can see the data is sorted. If I loop through each of the rows, the rows are not sorted.

 

Is there a way to sort the DataView so that the rows are sorted?

C# code

DataView tmp = myData.Tables["ATable"].DefaultView;
tmp.Sort = "SomeColumn";
dataGridView1.DataSource = tmp.Table;

// Loop through each row in the tmp var. They are not sorted, 
// but the data grid is.
foreach (DataRow currentRow in tmp.Table.Rows)
{
  string rowCol1 = currentRow[1].ToString();
}

 

The rows returned in the loop are not sorted. The datagrid on my forum is. Any ideas on how to get a loop through the rows that would be sorted?

 

Thank you,

Posted

I also tried setting the sort order before assigning it to my temp var. No luck.

 

According to the MSDN lib

Filtering and Sorting in Datasets

 

You can use a data view (DataView object). A data view is an object that acts as a layer on top of the data table, providing a filtered and sorted view of the table's contents.

 

Must be something simple I am doing wrong or is binding the data the only way to see the sorted view?

Posted

After spending time looking through docs and testing out "guesses", I now believe that you can use the default view and table object to show sorted values. Only the original order of the data.

 

If you want to see the sorted values you have to either "Select" the data into data rows or bind them to an object such as the grid. This doesn't seem right to be me, but at least working that way I can get the code moving again.

  • 2 years later...
Posted

Hello!

 

I'm facing the exactly same problem here...it's getting very frustrating as a matter of fact! What in the world is the use of this "Sort" property of the datatable's defaultview if it does not sort the rows? If someone find it out, please post the explanation here...

 

:mad:

  • Administrators
Posted

Have you tried using the DataView object as the data source and as the variable you iterate over rather than it's table property?

 

e.g.

DataView tmp = myData.Tables["ATable"].DefaultView;
tmp.Sort = "SomeColumn";
dataGridView1.DataSource = tmp;

// Loop through each row in the tmp var. They are not sorted, 
// but the data grid is.
foreach (DataRow currentRow in tmp)
{
  string rowCol1 = currentRow[1].ToString();
}

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

Posted

Finally!

 

Thank you! Your solution is fine!

 

My mistake when accessing the DataView was: DataTable.DefaultView.Table.Rows[line][column]...you see, DataTable.DefaultView.Table.Rows is the same of DataTable.Rows. Once you've sorted the DefaultView you must access it as showed by "PlausiblyDamp" (by looping through the DefaultView rows)

 

foreach (DataRowView currentRow in tmp)

{

string rowCol1 = currentRow[1].ToString();

}

 

Thank you again "PlausiblyDamp".

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...