sorting number in ascending way but still mixes the numbers

R

Richard

When i try sorting in the database, it sorts the numbers:
0
1
102
2
304
305
4
etc....

but i want to have it sorted like:
0
1
2
4
102
304
305

What must i do to make it so?
I just use a normal datagrid with sorting capabilities enabeld.

thx in advance

Richard
 
S

Steven Cheng[MSFT]

Hi Richard,

As for the sorting problem you mentioned, I also agree with Curt_C that the
problem is likely due to the DataTable's Column type. How do you add the
DataColumn into DataTable? The DataColumn has several constructors which
mapping to the DataTable.Columns.Add methods such as

Add(string)
Add(string, Type)
......

#DataColumn Constructor
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/
frlrfsystemdatadatacolumnclassctortopic.asp

and if you add the column as
DataTable.Columns.Add("column name");
then, the column is added as a stirng type column and when being sorting,
the column's data will be sort as ASCII index sorting.
So if you want to have a int or float type column being sort by number, you
need to add this column and specify the column type explicitly, such as

DataTable.Columns.Add("int column", typeof(int));
DataTable.Columns.Add("float column",typeof(float));

Please have a check. Hope helps. Thanks.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Get Preview at ASP.NET whidbey
http://msdn.microsoft.com/asp.net/whidbey/default.aspx
 
R

Richard

I dont know how to implement this,

my code for the datagrid is,
----
<asp:DataGrid runat="server" id="myDataGrid" autogeneratecolumns="false"
Border="0" ShowHeader="True"
AllowPaging="True" HorizontalAlign="center" PageSize="50"
PagerStyle-Mode="NumericPages" PagerStyle-PageButtonCount="100"
PagerStyle-HorizontalAlign="center" DataKeyField="U_ID"
AllowSorting="True" OnSortCommand="myDataGridMain_Sorting"
OnItemDataBound="myDataGrid_ItemDataBound" width="100%"
OnPageIndexChanged="myDataGrid_PageIndexChanged">
<columns>
<asp:BoundColumn DataField="A_LEVELNAAM" SortExpression="A_LEVELNAAM"
HeaderText="<strong>Level</strong>" />
<asp:BoundColumn DataField="U_Posts" SortExpression="U_Posts"
HeaderText="<strong>Posts</strong>" />
<asp:BoundColumn DataField="U_Registered_On"
SortExpression="U_Registered_On" HeaderText="<strong>Join Date</strong>"
DataFormatString="{0:dd-M-yyyy}" />
<asp:BoundColumn DataField="U_Last_Logged_In"
SortExpression="U_Last_Logged_In" HeaderText="<strong>Last Active</strong>"
DataFormatString="{0:dd-M-yyyy}" />
</columns>
</asp:DataGrid>
----
The field i want to sort this way is the U_Posts. I use that field for
whenever a person posts a item on the website, it does +1. When i look at
the users information i just want to see what user made the most posts on
the website etc.

Do you have a way how i can implement your idea into this?

my code behind for the databind()
----------------------
Sub BindData(ByVal sortExpr As String)
Dim ds As New DataSet
Dim dsc As SqlDataAdapter
Dim strSQL As String
Dim strCon As String

'strSQL = "SELECT * FROM tbl_Users ORDER BY " & sortExpr & ""
strSQL = "SELECT tbl_Users.*, tbl_Level.A_LEVELID,
tbl_Level.A_LEVELNAAM FROM tbl_Users INNER JOIN tbl_Level ON
tbl_Users.U_ToegangsID = tbl_Level.A_LEVELID ORDER BY " & sortExpr & ""
strCon = ConfigurationSettings.AppSettings("DigifanDatabaseCon")

dsc = New SqlDataAdapter(strSQL, strCon)
dsc.Fill(ds, "tbl_Users")

myDataGrid.DataSource = ds.Tables("tbl_Users").DefaultView
myDataGrid.DataBind()
End Sub
--------------------


Thx in advance
 
S

Steven Cheng[MSFT]

Hi Richard,

Thanks for your response and the detailed code you provided. Since you use
DataAdapter to fill dataset. So the DataColumns's type is auto set by
Adapter when filling the datas into dataTable in the DataSet. I think you
can add some additional code ( or use F5 debug ) to have a check on the
DataTable's colum
s DataType( for the "U_Posts" field). If the columns's DataType is string
rather than numeric type, when sorting, the data will be sorted by ascii
rather than numeric value. So please have a check on it.
(You can add the checking code after the dataadapter fill in the dataset
and before bind the dataset to the datagrid. )
In addition, what's the "U_Posts" column's type in the DataBase?


Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Get Preview at ASP.NET whidbey
http://msdn.microsoft.com/asp.net/whidbey/default.aspx
 

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

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top