Datagrid Sort Question


J

Jennifer

I've got a datagrid. The sort is working. But the columns that are
numeric are being sorted alphabetically. For example: 1,2,10,20 would
come out 1,10,2,20. Not sure how to get around this. I'm new to
datagrids in Visual Studio 2005. My sort code is below. Any
suggestions would be appreciated.

Thanks,
Jennifer

Private Sub dgDSR_SortCommand(ByVal source As Object, ByVal e As
System.Web.UI.WebControls.DataGridSortCommandEventArgs) Handles
dgDSR.SortCommand
Dim SortExpression As String
Dim SortType As String

If txtUnitID.Text = "" Or txtStartDate.Text = "" Then Exit Sub

SortType = ""
SortExpression = e.SortExpression.ToString

If SortExpression <> "" Then
If rbSort.Items(0).Selected = True Then
SortType = SortExpression & " ASC"
Else
SortType = SortExpression & " DESC"
End If
End If
FillGrid(SortType)
End Sub
 
Ad

Advertisements

J

Jennifer

I've figured it out. Posting answer in case anyone else needs to
know.

In the code where the datagrid is filled, set the data type of each
column as needed as below.

dtMain.Columns(4).DataType = GetType(Decimal)

Entire sub routine code below:

Private Sub FillGrid(ByVal SortExp As String)
Dim dvMain As DataView
Dim X As Int64
Dim dtMain As New DataTable()
Dim DR As DataRow
Dim Update_User As String

If txtStartDate.Text <> "*" Then
If txtEndDate.Text = "*" Then Exit Sub
End If

dvMain = GetDailySales(txtUnitID.Text, txtStartDate.Text,
txtEndDate.Text)

Dim Col1, Col2, Col3, Col4, Col5, Col6, Col7, Col8, Col9,
Col10 As String


Col1 = "Unit"
Col2 = "Date"
Col3 = "Status"
Col4 = "Deposit" & vbCrLf & "Evening" & vbCrLf & "$"
Col5 = "Deposit" & vbCrLf & "Early_Bird" & vbCrLf & "$"
Col6 = "Deposit" & vbCrLf & "Day" & vbCrLf & "$"
Col7 = "Deposit" & vbCrLf & "Special" & vbCrLf & "$"
Col8 = "Deposit" & vbCrLf & "Total" & vbCrLf & "$"
Col9 = "Credit_Card" & vbCrLf & "Visa/Debit" & vbCrLf & "$"
Col10 = "Credit_Card" & vbCrLf & "MasterCard" & vbCrLf & "$"

dtMain.Columns.Add(Col1)
dtMain.Columns.Add(Col2)
dtMain.Columns.Add(Col3)
dtMain.Columns.Add(Col4)
dtMain.Columns.Add(Col5)
dtMain.Columns.Add(Col6)
dtMain.Columns.Add(Col7)
dtMain.Columns.Add(Col8)
dtMain.Columns.Add(Col9)
dtMain.Columns.Add(Col10)


dtMain.Columns(0).DataType = GetType(Integer)
' dtMain.Columns(1).DataType = GetType(Date)
dtMain.Columns(4).DataType = GetType(Decimal)
dtMain.Columns(5).DataType = GetType(Decimal)
dtMain.Columns(6).DataType = GetType(Decimal)
dtMain.Columns(7).DataType = GetType(Decimal)
dtMain.Columns(8).DataType = GetType(Decimal)
dtMain.Columns(9).DataType = GetType(Decimal)
Dim BD As Date
For X = 0 To dvMain.Count - 1

DR = dtMain.NewRow
DR(Col1) = CInt(dvMain(X)("Unit_ID"))
BD = dvMain(X)("Business_Date")
DR(Col2) = BD.ToShortDateString
DR(Col3) = dvMain(X)("Status")
DR(Col4) = Format(dvMain(X)("Deposit_Eve_Amt"), "N")
DR(Col5) = Format(dvMain(X)("Deposit_EB_Amt"), "N")
DR(Col6) = Format(dvMain(X)("Deposit_Day_Amt"), "N")
DR(Col7) = Format(dvMain(X)("Deposit_Special_Amt"), "N")
DR(Col8) = Format(dvMain(X)("Deposit_Eve_Amt") + dvMain(X)
("Deposit_EB_Amt") + dvMain(X)("Deposit_Day_Amt") + dvMain(X)
("Deposit_Special_Amt"), "N")
DR(Col9) = Format(dvMain(X)("CC_Visa_Debit_Amt"), "N")
DR(Col10) = Format(dvMain(X)("CC_MasterCard_Amt"), "N")

dtMain.Rows.Add(DR)

Next
Dim dv As New DataView(dtMain)
dv.Sort = SortExp
dgDSR.DataSource = dv

dgDSR.DataBind()
dgDSR.ItemStyle.Wrap = False
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top