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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

datagrid columns aren't sorting 1

Status
Not open for further replies.

kisslogicgoodbye

Programmer
Mar 19, 2001
18
US
I have a datagrid. "AllowSorting" is true. My column headings are normal underlined links. When they are clicked the page flickers as if data is being refreshed. The column sort doesn't change. Can any one help?


VB Code
Protected Sub DataGrid1_SortCommand(ByVal source As Object, _
ByVal e As System.Web.UI.WebControls.DataGridSortCommandEventArgs) _
Handles DataGrid1.SortCommand
DataView1.Sort = e.SortExpression
DataGrid1.DataBind()
End Sub


Asp code:
<asp:datagrid id=DataGrid1 style=&quot;Z-INDEX: 101; LEFT: 21px; POSITION: absolute; TOP: 24px&quot; runat=&quot;server&quot; ForeColor=&quot;Black&quot; BorderColor=&quot;#999999&quot; BackColor=&quot;#CCCCCC&quot; AllowSorting=&quot;True&quot; OnSortCommand=&quot;DataGrid1_SortCommand&quot;
 
All of the asp code...
<form id=&quot;Form1&quot; method=&quot;post&quot; runat=&quot;server&quot;>
<asp:datagrid id=DataGrid1 style=&quot;Z-INDEX: 101; LEFT: 21px; POSITION: absolute; TOP: 24px&quot; runat=&quot;server&quot; ForeColor=&quot;Black&quot; BorderColor=&quot;#999999&quot; BackColor=&quot;#CCCCCC&quot; AllowSorting=&quot;True&quot; OnSortCommand=&quot;DataGrid1_SortCommand&quot; Width=&quot;817px&quot; Height=&quot;596px&quot; DataKeyField=&quot;Restaurant_ID&quot; DataMember=&quot;Restaurant&quot; DataSource=&quot;<%# dsRestaurants1 %>&quot; AutoGenerateColumns=&quot;False&quot;>
<SelectedItemStyle Font-Bold=&quot;True&quot; ForeColor=&quot;White&quot; BackColor=&quot;#000099&quot;></SelectedItemStyle>
<ItemStyle BackColor=&quot;White&quot;></ItemStyle>
<HeaderStyle Font-Bold=&quot;True&quot; ForeColor=&quot;White&quot; BorderStyle=&quot;Outset&quot; BackColor=&quot;Black&quot;></HeaderStyle>
<FooterStyle BackColor=&quot;#CCCCCC&quot;></FooterStyle>
<Columns>
<asp:EditCommandColumn ButtonType=&quot;PushButton&quot; UpdateText=&quot;Update&quot; CancelText=&quot;Cancel&quot; EditText=&quot;Edit&quot;></asp:EditCommandColumn>
<asp:BoundColumn DataField=&quot;Name&quot; SortExpression=&quot;Name&quot; HeaderText=&quot;Name&quot;></asp:BoundColumn>
<asp:BoundColumn DataField=&quot;Type&quot; SortExpression=&quot;Type&quot; HeaderText=&quot;Type&quot;></asp:BoundColumn>
<asp:TemplateColumn SortExpression=&quot;PriceRageLow&quot; HeaderText=&quot;Price Range Low&quot;>
<ItemTemplate>
<asp:Label id=Label1 runat=&quot;server&quot; Text='<%# DataBinder.Eval(Container, &quot;DataItem.PriceRageLow&quot;, &quot;{0:C}&quot;) %>'>
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox id=TextBox1 runat=&quot;server&quot; Width=&quot;149px&quot; Text='<%# DataBinder.Eval(Container, &quot;DataItem.PriceRageLow&quot;, &quot;{0:C}&quot;) %>'>
</asp:TextBox>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:BoundColumn DataField=&quot;PriceRangeHigh&quot; SortExpression=&quot;PriceRangeHigh&quot; HeaderText=&quot;Price Range High&quot; DataFormatString=&quot;{0:C}&quot;></asp:BoundColumn>
</Columns>
<PagerStyle HorizontalAlign=&quot;Left&quot; ForeColor=&quot;Black&quot; BackColor=&quot;#CCCCCC&quot; Mode=&quot;NumericPages&quot;></PagerStyle>
</asp:datagrid></form>
 
You're saying this in the handler:

DataView1.Sort = e.SortExpression

which would appear to me to say that the datasource for your datagrid would be DataView1, but then you declare the datasource for your datagrid to be:

DataSource=&quot;<%# dsRestaurants1 %>&quot;

That would have to be the problem. Consider this logic:

private sortExpression as string

page_load
if not page.ispostback then
bindGrid()
end if
end page_load

private sub bindGrid()
dim dv as dataview
dv = returnDataView()
if not sortExpression is nothing then
dv.sort = sortExpression
end if
DataGrid1.datasource = dv
DataGrid1.databind()
end sub

sort_handler
sortExpression = e.sortExpression
bindGrid()
end sort_handler

private function returnDataView() as dataview
'your data access block goes in here
' you should pull the dataset and then extract the
' .defaultView() from it, and return it to the caller
end function

I omitted the long sub signatures and such for readability. But basically, take the datasource property out of the declaration for the datagrid, and leave it completely to the codebehind. In this case, you declare a class variable, sortExpression, which is only initialized when the sort event fires. Then you check to see if it was initialized and apply it to the dataview if it was.

This type of logic works very well -- any time any of your other events need to bind, you just call bindGrid() and you're done. Very simple and clean approach, which I highly recommend.

Additionally, I invite you to check out the extension of the datagrid that has built in sorting capabilities with auto-reverse, as well. It's implemented as a custom server control which inherits from the datagrid and adds a few extra properties which take advantage of the viewstate bag to make sorting a breeze. I put it in the following FAQ:
faq855-2026

good luck! :)
paul
penny1.gif
penny1.gif
 
If you like it so much give the poor man a star. That'l do donkey, that'l do
[bravo]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top