数字不按顺序排序

时间:2015-01-01 20:39:00

标签: c# asp.net sql-server gridview

我尝试使用asp.net和c#按顺序(1到...)对列进行排序。在做了一些研究后,似乎我需要将SQL Server中的列类型更改为int但是这不可能,因为列存储门牌号码,我最终可能会有一个门牌号码10a(例如)所以当前设置到varchar。因此,它没有正确排序列。我已经尝试将相关列作为int进行转换,但是当运行应用程序时,我在gridview中遇到了它的绑定错误,我还尝试了其他这个作为* 1但在运行时仍然在绑定部分上出错

SQL语句

    SELECT DISTINCT tblcontact.ContactID, tblcontact.Forename, tblcontact.Surname, 
    tbladdress.[House Number], tbladdress.AddressLine1, tbladdress.AddressLine2, 
    tblcontact.[Business Name] FROM tblcontact INNER JOIN tbladdress ON tblcontact.AddressID = tbladdress.AddressID 
    LEFT OUTER JOIN tblDonate 
    ON tblcontact.ContactID = tblDonate.ContactID 
    WHERE (tbladdress.CollectionArea = @CollectionArea) AND 
(tbladdress.AddressLine1 = @drpCollectionStreet) 
    ORDER BY tbladdress.[House Number] ASC

Gridview标记

                                    <asp:TemplateField HeaderText="House Number">
                                        <EditItemTemplate>
                                            <asp:TextBox ID="txtHouseNum" runat="server" Text='<%# Bind("[House Number]") %>'></asp:TextBox>
                                        </EditItemTemplate>
                                        <ItemTemplate>
                                            <asp:Label ID="lblHouseNum" runat="server" Text='<%# Bind("[House Number]") %>'></asp:Label>
                                        </ItemTemplate>
                                    </asp:TemplateField>

以下是其排序方式的示例

21
22
27
28
5
6
8
9

这是我如何转换为int,它在SQL中排序很好,但我在Bind上得到错误(上图)

SELECT DISTINCT tblcontact.ContactID, tblcontact.Forename, tblcontact.Surname, cast(tbladdress.[House Number] as int), 
tbladdress.AddressLine1, tbladdress.AddressLine2, tblcontact.[Business Name] 
FROM tblcontact INNER JOIN tbladdress ON tblcontact.AddressID = tbladdress.AddressID 
LEFT OUTER JOIN tblDonate ON tblcontact.ContactID = tblDonate.ContactID 
WHERE (tbladdress.CollectionArea = 'Queens Park') AND (tbladdress.AddressLine1 = 'Kings Road') 
ORDER BY cast(tbladdress.[House Number] as int)

运行时错误

这是运行时的错误(这是最有意义的)

 "House Number is neither a DataColumn nor a DataRelation for table DefaultView."} 

3 个答案:

答案 0 :(得分:1)

要以数字方式对街道号码进行排序,您需要将它们作为数字投射。当然,麻烦的是诸如“10a”之类的字符串不能被转换为INT。答案是从[House Number]列中提取数字字符,然后进行演员表。以下是对ORDER BY子句执行此操作:

... 
ORDER BY CAST(SUBSTRING(tbladdress.[House Number], PATINDEX('%[0-9]%', tbladdress.[House Number]),
                 1 + PATINDEX('%[0-9][^0-9]%', tbladdress.[House Number] + ' ') -
                 PATINDEX('%[0-9]%',tbladdress.[House Number])) AS INT)

请参阅http://blogs.lessthandot.com/index.php/DataMgmt/DataDesign/extracting-numbers-with-sql-server/,了解复杂外观公式的工作原理。

答案 1 :(得分:1)

从选择cast中删除column list。只保留在order by,它会按顺序排列结果。

select A.ContactID,
                A.Forename,
                A.Surname,
                A.[House Number],
                A.AddressLine1,
                A.AddressLine2,
                A.[Business Name] from (
SELECT DISTINCT tblcontact.ContactID,
                tblcontact.Forename,
                tblcontact.Surname,
                tbladdress.[House Number],
                tbladdress.AddressLine1,
                tbladdress.AddressLine2,
                tblcontact.[Business Name]
FROM   tblcontact
       INNER JOIN tbladdress
               ON tblcontact.AddressID = tbladdress.AddressID
       LEFT OUTER JOIN tblDonate
                    ON tblcontact.ContactID = tblDonate.ContactID
WHERE  ( tbladdress.CollectionArea = 'Queens Park' )
       AND ( tbladdress.AddressLine1 = 'Kings Road' )) A
ORDER  BY Cast(tbladdress.[House Number] AS INT) 

答案 2 :(得分:0)

正确答案是JohnS和NoDisplayName答案的组合,这要归功于你们两个

select A.ContactID,
                A.Forename,
                A.Surname,
                A.[House Number],
                A.AddressLine1,
                A.AddressLine2,
                A.[Business Name] from (
SELECT DISTINCT tblcontact.ContactID,
                tblcontact.Forename,
                tblcontact.Surname,
                tbladdress.[House Number],
                tbladdress.AddressLine1,
                tbladdress.AddressLine2,
                tblcontact.[Business Name]
FROM   tblcontact
       INNER JOIN tbladdress
               ON tblcontact.AddressID = tbladdress.AddressID
       LEFT OUTER JOIN tblDonate
                    ON tblcontact.ContactID = tblDonate.ContactID
WHERE  ( tbladdress.CollectionArea = 'Queens Park' )
       AND ( tbladdress.AddressLine1 = 'Kings Road' )) A
       ORDER BY CAST(SUBSTRING(A.[House Number], PATINDEX('%[0-9]%', A.[House Number]),
                 1 + PATINDEX('%[0-9][^0-9]%', A.[House Number] + ' ') -
                 PATINDEX('%[0-9]%',A.[House Number])) AS INT)