sorting datagrid columns

  • Thread starter Thread starter Brock
  • Start date Start date
B

Brock

I have a successfully functioning DataGrid that I'd like to add
sorting capabilities to. No editing is allowed to the data. As it
stands I have 4 columns of data "Emp #", "Last Name", "First Name",
and "Title". Each of these displays also as the names of the column
headers displayed. How can I using my working code below add sorting
functionality that allows the users to click the column name and with
each click produce a sort? The datagrid would default to Emp #, but
either of the four columns could be made to sort. each click would
alternate between ASC and DESC order with ASC being the default.
Thanks for any clues! Brock

Private Sub SetListViewItem(ByVal dr As Employee, ByVal dt As
DataTable, ByVal count As Integer)
If count = 0 Then
dt.Columns.Add("Emp #")
dt.Columns.Add("Last Name")
dt.Columns.Add("First Name")
dt.Columns.Add("Title")
End If
Dim EmpPos As Action = (New
ActionBroker).GetActionCurrent(dr.Key, Today, Global.UserName,
Global.UserPassword, Global.appDataSource)
Dim employee As DataRow = dt.NewRow
employee("Emp #") = dr.Key
employee("Last Name") = dr.LastName
employee("First Name") = dr.FirstName
employee("Title") = EmpPos.WorkAgainstInfo.Title
dt.Rows.Add(employee)
End Sub 'SetListViewItem
 
each click produce a sort? The datagrid would default to Emp #, but
either of the four columns could be made to sort. each click would
alternate between ASC and DESC order with ASC being the default.


Look at the event SortCommand.
You can make use of ViewState to store:

1) Current column on which the sorting is being done.
2) The order.

If you're unable to get the exact code, let me know. I'll post the code :)
 
One thing I'm confused on, (I've looked for SortCommand examples that
seem inconsistent with what I'm doing)
please help me here. I've of course placed the asp:datagrid as a tool
within my html:

<asp:datagrid id="dgEmployees" runat="server" AllowSorting="True"
OnItemCommand="ShowDetails" DataKeyField="emp #">
<Columns>
<asp:ButtonColumn Text="Select" CommandName="SelectItem"></
asp:ButtonColumn>
</Columns>
</asp:datagrid>

However I've really only defined there the "ButtonColumn". All the
other columns I configured in my "code-behind" file:

Private Sub SetListViewItem(ByVal dr As Employee, ByVal dt As
DataTable, ByVal count As Integer)
If count = 0 Then
dt.Columns.Add("Emp #")
dt.Columns.Add("Last Name")
dt.Columns.Add("First Name")
dt.Columns.Add("Title")
End If
Dim EmpPos As Action = (New
ActionBroker).GetActionCurrent(dr.Key, Today, Global.UserName,
Global.UserPassword, Global.appDataSource)
Dim employee As DataRow = dt.NewRow
employee("Emp #") = dr.Key
employee("Last Name") = dr.LastName
employee("First Name") = dr.FirstName
employee("Title") = EmpPos.WorkAgainstInfo.Title
dt.Rows.Add(employee)
End Sub 'SetListViewItem

Thus I'm trying to figure out where to go in my code based on this
structure I set up. In short, where to use the "SortCommand" ?

Thanks!!
 
Back
Top