Sorting dataView without DataGrid in Web Project

invisigoth

Member
Joined
Dec 10, 2002
Messages
6
Location
Canada
Hi everybody

I have a problem with the DataView object in our current web project. We dont use DataGrid controls. In one spot in the code, we have to fetch data from various sources (SQL + Xml + others) and I build a DataTable with specific column names. I generate a custom-built web control, from tables and other things to present the data to the UI. In the event of a sort, I thought that sorting on the dataView would be ok, but it doesnt. In fact, it doesnt work at all. ViewState is set to false throughout our entire project.

Is there something missing in the sample I provide? Am I missing something? I have been searching on the various forums and newsgroups, and other resources, but without success. Please help me, anyone.

Thanks in advance!

Iz

Sample code in VB:


Code:
objMyDT is a dataTable
objDataView is what I will use to populate row by row an
html structure that isnt a datagrid object.

strMySortString = "MyColumn1 DESC"
objDataView = objMyDT.DefaultView

If strMySortString.Length > 0 Then     We have a sort?
    objDataView.Sort = strMySortString
End If

Dim myDataRow as DataRow
For iCnt = iStartCnt To iEndCnt        get some results 
     Get current DataRow:
    myDataRow = objDataView.Table.Rows(iCnt)
    ... here would be the code where I add TableCells 
    ...    to TableRows, here irrelevant.
    ...
Next
 
Last edited by a moderator:
Since this is a web project, are you sure this code is being executed? The data isnt going to sort client-side, so in other words youre going to need to post the page back to the server in the event that the user requests a resort. Also, turn view state on for the page. Controls will lose their settings otherwise.

I am just guessing here though. Other things could very well be causing this.
 
First, thanks for replying so quickly. So far, this is the only site I had answers from.

About ViewState, no can do, it has to be off. We maintain the settings of the controls in a very different way, and there is no way we loose them.

What I was talking about in my question is strictly code behind and server side.

And yes, I can assure you that this code is being executed.
 
hmm, I just tested objDataView.Sort = strMySortString, and it does sort the dataview.
Are you getting an error?
 
No error, it just does not sort. The rows stay in the same order. Maybe it is a setting of the DataView I am not applying ?
I just found something that may answer my question, though.

Im looking through the doc for RowFilters of dataView. If I find something on that track, Ill post the solution, if I find it.

Thanks

Iz
 
I just thought of something, since the sort is based on a client side click (or something) then why not in the New

ie ... objDataView = New DataView(....)

pass the sort order, and if the strMySortString is null or not a Postback you can set a default value for it.
 
Hi

here is an update. To answer the previous question, I was using exactly the same type of options there is for the constructor as it is in the documentation:
Code:
Either
dvDataView = New DataView(dtMyDT) 
dvDataView.Sort="ProductName DESC"
Or
dvDataView = New DataView(dtMyDT, "ProductID>1", "ProductName DESC", DataViewRowState.CurrentRows)
There is no difference.
What I did next was to trace (using VS.NET and a watch on my dataview) the code. Even if I use a dataGrid object and bind the thing to it as its datasource, if I look inside the value of the dataview (with a watch) and kinda drill down to the rows, they are not sorted. If I loop through them with whatever other object, they are not sorted. The only evidence of sorting is when I view the datagrid I binded the dataview to.
Code:
        Dim objMyCustomDGTable as new Table()
        Dim objTableRow As DataRow
        build headers
        objTR = New TableRow()
        For iCnt2 = 0 To iMax2
            objTD = New TableCell()
            objTD.Text = "<b>" & dvDataView.Table.Columns(iCnt2).ColumnName & "</b>"
            objTR.Controls.Add(objTD)
        Next
        objMyCustomDGTable.Controls.Add(objTR)
        build rows
        For iCnt = 0 To iMax1
            objTR = New TableRow()
            objTableRow = dvDataView.Table.Rows(iCnt)
            For iCnt2 = 0 To iMax2
                objTD = New TableCell()
                objTD.Text = CType(objTableRow.Item(iCnt2), String)
                objTR.Controls.Add(objTD)
            Next
            objMyCustomDGTable.Controls.Add(objTR)
        Next
        objPH1.Controls.Add(objMyCustomDGTable)objPH1 is a place holder control I declared on the form

Ideas? anyone?

thanks again in advance!

Iz
 
Your problem lies in how youre getting to the rows, through the Table property. This skips the DataView and goes right back to the original table in the order that it has things, not the DataView.

Try replacing
objTableRow = dvDataView.Table.Rows(iCnt)
with
objTableRow = dvDataView(iCnt)

-Nerseus
 
Problem solved - Thank you

What you posted worked very well. Thanks also for the quick explanation. Ill look at the DataRowView objects a little closer.

Thank you so much! Thank you all of you.

Iz
 
Back
Top