Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Unable to sort the field in DataView 1

Status
Not open for further replies.

fletchsod

Programmer
Dec 16, 2002
181
I have a problem. I'm unable to sort the field in DataView. I did Google surfing and found that DataSet cannot be sorted (field) cuz it is tied in to DataSource and that we would have to iterate over DataView instead. So, this is what I did and it still doesn't work.

Code:
oDataView = oDataSet.Tables[0].DefaultView;
oDataView.Sort = "GrossProfit2 DESC";

while (x < xLength) {
    Response.Write("<tr>\n");
    Response.Write(" <td>" + oDataView.Table.Rows[x]["Year2"].ToString() + "</td>\n");
    Response.Write(" <td>" + oDataView.Table.Rows[x]["Make2"].ToString() + "</td>\n");
    Response.Write(" <td>" + oDataView.Table.Rows[x]["Model2"].ToString() + "</td>\n");
    Response.Write(" <td>" + oDataView.Table.Rows[x]["Style2"].ToString() + "</td>\n");
    Response.Write(" <td>" + oDataView.Table.Rows[x]["GrossProfit2"].ToString() + "</td>\n");
    Resposne.Write("</tr>\n");

    x += 1;
}
[code]

I welcome some helps on fixing the sorting bug to the script I made.
 
try:
Code:
Dim oDataView As New DataView( oDataSet.Tables[0])
    oDataView.Sort = "GrossProfit2 DESC"
 
DataSource": first problem. you loose all control when you use datasource controls.

if you bind the gridview to a sorted dataview everything will work just fine.
Code:
var table = GetDataTable();
var view = table.DefaultView;
view.Sort = "[column] asc/desc";
mygrid.DataSource = view;
DataBind();
where GetDataTable is function that returns the datatable.

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
faq732-7259
 
I went ahead and tried both of your sample script and it still doesn't work. :-/

I'm not familiar with the GetDataTable() function but I assumed you meant existing data as an example.

I'm not using databinding or DataSource. I just pour the data into DataSet from Sql's DataAdapter.
 
GetDataTable is code you write.

we need to take a step back. what do you mean by "doesn't work". an exception and stacktrace will go a long way to explain what the problem is.

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
faq732-7259
 
No exception error. Just that when I tell it to sort by this field in ascending order, it doesn't sort at all.

The DataSet itself is a DataSet where it kept on collecting data from 5 seperate sql-queries. Here's the sample I used (see below)...

Code:
oSqlDataAdapter.Fill(oDataSet_Source, sAsk);

if (oDataSet_Target.Tables.Count == 0) {
   oDataSet_Target = oDataSet_Source;
} else {
   foreach(DataRow oDataRow in oDataSet_Source.Tables[0].Rows) 
   {
      oDataSet_Target.Tables[0].ImportRow(oDataRow);
   }
}

I can verify the data works cuz I see them on the webpage. Just that no matter what I do with sorting, the DataView does not sort the rows by field.
Thanks...
 
you are binding the data to the view, not the table correct? a dataview is a snapshot of the table, not the table itself. sorting the view has no effect on the table.

another option. drop the use of datasets/tables altogether and map the data to POCOs (plain old compiled objects) put the pocos in a collection and use linq.
Code:
var list = new List<Entity>();
list.AddRange(GetEntitiesFromSource1());
list.AddRange(GetEntitiesFromSource2());
list.AddRange(GetEntitiesFromSource3());
list.AddRange(GetEntitiesFromSource4());
list.AddRange(GetEntitiesFromSource5());
return list
   .OrderBy(e=>e.Foo)
   .ThenByDescending(e=>e.Bar)
   .AsEnumerable();

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
faq732-7259
 
How do I convert this back to System.Data.DataTable? That might be all I need... I'm not familiar with LINQ.

Code:
var table = GetDataTable();
var view = table.DefaultView;
view.Sort = "[column] asc/desc";

Thanks.
 
you don't need a datatable to display the data. just bind directly to the dataview.

if the control requires a datatable. use the built-in functionality of the data view.
Code:
var view = table.DefaultView;
view.Sort = "...";
var sortedTable = view.ToTable();

mycontrol.DataSource = sortedTable;
DataBind();



Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
faq732-7259
 
That works now... Now I can loop through the "sortedTable" and display the html table in proper field's order by row.

Thanks...

 
P.S. - Do you know why when sorting a field in a table require assigning the result to a new table for displaying? (when the existing table had already been sorted and should be displayable this way)

Thanks...
 
a data table is just data, similar to a table in a database. a data view is a snap shot of the table. similar to a query (or view if the db has it). you can sort/filter the results without effecting the actual data. this way you could potentially view the table many different ways from a single source. this is all possible without affecting the underlying data.

DataView.ToTable() creates a new table to give you the full capabilities of a data table without modifying the original data source.

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
faq732-7259
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top