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 sorting not working

Status
Not open for further replies.

sthmpsn1

MIS
Sep 26, 2001
456
US
My datagrid sorting feature isn't working. The titles in my datagrid are not underlined for sorting. What am I doing wrong??
Here is my code

<HEAD>
<title>Lets give it a try</title>
<script runat=&quot;server&quot;>
Public Sub Page_Load(Source as Object, E As EventArgs)
if Not Page.IsPostBack Then
BindData( &quot;Name&quot; )
End If
End Sub

Public Sub BindData( strSortField As String)
Dim myDataSet as New DataSet
Dim mySqlDataAdapter as SqlDataAdapter
mySqlDataAdapter = New SqlDataAdapter( _
&quot;SELECT * FROM Person order by &quot; & strSortField, _
&quot;server=AM1ST_FS1;database=HRINFO;uid=sa;&quot;)
mySqlDataAdapter.Fill(myDataSet, &quot;Person&quot;)
PersonInfo.DataSource = myDataSet.Tables(&quot;Person&quot;)
PersonInfo.DataBind()
End Sub

public Sub DataGrid_Sort(Source As Object, E As DataGridSortCommandEventArgs)
BindData( E.SortExpression )
End Sub

Public Sub DataGrid_Edit(Source As Object, E As DataGridCommandEventArgs)
personInfo.EditItemIndex= E.Item.ItemIndex
BindData( &quot;Name&quot; )
End Sub

Public Sub DataGrid_Cancel(Source As Object, E As DataGridCommandEventArgs)
personInfo.EditItemIndex = -1
BindData( &quot;Name&quot; )
End Sub

Public Sub DataGrid_Update(Source As Object, E As DataGridCommandEventArgs)
Dim myConnection As SqlConnection
Dim myCommand As SqlCommand
Dim txtName As Textbox = E.Item.Cells(2).Controls(0)
Dim txtAddress As Textbox = E.Item.Cells(3).Controls(0)
Dim strUpdateStmt As String
strUpdateStmt = &quot;UPDATE Person SET Name = '&quot; & txtName.Text & &quot;', Address = '&quot; & txtAddress.Text & &quot;' WHERE ID = &quot; & E.Item.Cells(1).Text
myConnection = New SqlConnection(&quot;server=AM1ST_FS1;database=HRINFO;uid=sa&quot;)
myCommand = new SqlCommand(strUpdateStmt, myConnection)
myConnection.open()
myCommand.ExecuteNonQuery()

personInfo.EditItemIndex = -1
BindData( &quot;Name&quot; )
End Sub
</script>
</HEAD>
<body>
<form id=&quot;Form1&quot; method=&quot;post&quot; runat=&quot;server&quot;>
<h3>Editing Records in The DataGrid</h3>
<asp:DataGrid id=&quot;PersonInfo&quot; Runat=&quot;server&quot;
AllowSorting=&quot;True&quot;
AlternatingItemStyle-BackColor=&quot;#A6F4FD&quot;
AutoGeneratecolumns=&quot;false&quot;
onEditCommand=&quot;DataGrid_Edit&quot;
onCancelCommand=&quot;DataGrid_Cancel&quot;
onUpdateCommand=&quot;DataGrid_Update&quot;
onSortCommand=&quot;DataGrid_Sort&quot;>
<columns>
<asp:editcommandcolumn
ButtonType=&quot;LinkButton&quot;
CancelText=&quot;Cancel&quot;
editText=&quot;Edit&quot;
UpdateText=&quot;Update&quot; />
<asp:boundcolumn
DataField=&quot;ID&quot;
HeaderText=&quot;ID&quot;
ReadOnly=&quot;true&quot; />
<asp:boundcolumn
DataField=&quot;Name&quot;
HeaderText=&quot;Name&quot; />
<asp:boundcolumn
DataField=&quot;Address&quot;
HeaderText=&quot;Address&quot; />
</columns>
</asp:DataGrid>
</form>
</body>
</HTML>
 
You must specify a &quot;sortExpression&quot; for each column that you wish to be sortable. So that last column you have would wind up looking like this:

<asp:boundcolumn
DataField=&quot;Address&quot;
HeaderText=&quot;Address&quot;
SortExpression=&quot;Address&quot;/>
penny1.gif
penny1.gif
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top