By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,402 Members | 1,262 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,402 IT Pros & Developers. It's quick & easy.

sorting number in ascending way but still mixes the numbers

P: n/a
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
Nov 18 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Are they coming through as a numeric datatype or as strings?
--
Curt Christianson
Owner/Lead Developer, DF-Software
Site: http://www.Darkfalz.com
Blog: http://blog.Darkfalz.com
"Richard" <ri*****@nospam.com> wrote in message
news:uL****************@TK2MSFTNGP11.phx.gbl...
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

Nov 18 '05 #2

P: n/a
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/de...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
Nov 18 '05 #3

P: n/a
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("DigifanDatabase Con")

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

myDataGrid.DataSource = ds.Tables("tbl_Users").DefaultView
myDataGrid.DataBind()
End Sub
--------------------
Thx in advance
----
"Steven Cheng[MSFT]" <v-******@online.microsoft.com> schreef in bericht
news:kk**************@cpmsftngxa10.phx.gbl...
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/de...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

Nov 18 '05 #4

P: n/a
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

Nov 18 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.