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!

Unbound GridView Sort...

Status
Not open for further replies.

VisualGuy

Programmer
May 27, 2003
162
US
I'm completely confused...

I'm binding a RS to a Gridview in ASP.NET 3.5 via VB.NET 2008.

ReturnUserid = "SELECT X , Y, Z, FROM XYZ_TABLE WHERE X = '" & Trim(TextBox1.Text) & "' ORDER BY X"
Dim DSReturnUserid As New Data.DataSet
Dim adaptorReturnUserid As SqlDataAdapter = New SqlDataAdapter(ReturnUserid, StrCon)
Dim cmdBuilderReturnUserid As SqlCommandBuilder = _
New SqlCommandBuilder(adaptorReturnUserid)
adaptorReturnUserid.Fill(DSReturnUserid)

If DSReturnUserid.Tables(0).Rows.Count > 0 Then
'TextBox1.Text = DSReturnUserid.Tables(0).Rows(0).Item(0)
GridView1.DataSource = DSReturnUserid
GridView1.DataBind()
PleaseWait.Value = ""
Else
PleaseWait.Value = "No match found. Please try again."
End If

_________________________________________

This seems to work fine, except for the fact that I cannot perform a sort. My gridview allows for sorting...

<asp:GridView ID="GridView1" runat="server" BackColor="White"
BorderColor="#CC9966" BorderStyle="None" BorderWidth="1px" CellPadding="4"
AllowSorting="True" >
<FooterStyle BackColor="#FFFFCC" ForeColor="#330099" />
<RowStyle BackColor="White" ForeColor="#330099" />
<Columns>
<asp:ButtonField ButtonType="Button" Text="Expand Details" />
</Columns>
<PagerStyle BackColor="#FFFFCC" ForeColor="#330099" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="#663399" />
<HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="#FFFFCC" />
<AlternatingRowStyle BackColor="#99CCFF" />
</asp:GridView>

...and I'm handling the sorting in the code behind...

Protected Sub GridView1_Sorting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewSortEventArgs) Handles GridView1.Sorting
Dim oldExpression As String = GridView1.SortExpression
Dim newExpression As String = e.SortExpression

If (oldexpression.IndexOf(newExpression) < 0) Then
If (oldexpression.Length > 0) Then
e.SortExpression = newExpression & "," & oldexpression
Else
e.SortExpression = newExpression
End If
Else
e.SortExpression = oldExpression
End If
End Sub

...Why is this not working?
 
You need to rebind the grid to a sorted data source after you apply the sort.

Also, use a parameterised query if you really have to do it inline.

Mark,

[URL unfurl="true"]http://lessthandot.com[/url] - Experts, Information, Ideas & Knowledge
[URL unfurl="true"]http://mdssolutions.co.uk[/url] - Website Design
[URL unfurl="true"]http://aspnetlibrary.com[/url] - An online resource for professional ASP.NET developers
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top