简体   繁体   English

在C#中按从小到大的顺序显示数据库中的主键

[英]Displaying primary key from database in order from least to greatest in C#

In my database the primary keys are in order from least to greatest (1,2,3, etc.) but when I load them into my combobox they are displayed out of order. 在我的数据库中,主键的顺序是从最小到最大(1、2、3等),但是当我将它们加载到组合框中时,它们的显示顺序不正确。 I'm sure there is an easy fix to this, but I'm stuck. 我敢肯定有一个简单的解决办法,但是我被卡住了。

    public void fillComboBox()
    {
        try
        {
            // query to pull invoice ids
            string sql = "SELECT PersonalInfo.[ID] FROM PersonalInfo order by ID"; 

            // adapter to send query to database
            OleDbDataAdapter daInvoices = new OleDbDataAdapter(sql, Conn);
            // fill dataset
            daInvoices.Fill(cusNumDS, "PersonalInfo");

            // declare data source for invoice id combo box
            custCB.DataSource = cusNumDS.Tables[0];
            custCB.DisplayMember = "PersonalInfo";
            custCB.ValueMember = "ID";
            // bind data source to combo box
            custCB.DataBindings.Add("SelectedValue", cusNumDS.Tables[0], "ID");
        }
        catch (Exception ex)
        {
            // error message if trouble pulling invoice ids from database
            MessageBox.Show("Trouble pulling invoices from database because: " + ex,
                "Database Error",
                MessageBoxButtons.OK,
                MessageBoxIcon.Error);
        }
    }
SELECT PersonalInfo.[ID] FROM PersonalInfo order by 1 ASC

尝试这个....

From a comment, you say.. 从评论中,你说..

No the combobox displays everything that begins with 1, such as 1, 10, 11, 12...2, 20, 21, 22 etc. I want it to be in order from lowest to highest. 没有组合框显示以1开头的所有内容,例如1、10、11、12 ... 2、20、21、22等。我希望它按从低到高的顺序排列。

This means a Lexicographical Ordering for text is being used during the sorting. 这意味着在排序过程中将使用文本的词典顺序 Now, there are two reasons (I can think of) that would cause this. 现在,有两个原因(我能想到)会导致这种情况。

  1. The KEY might be a char/varchar column and the ordering is applied by the database. KEY可能是char / varchar列,并且该顺序由数据库应用。 If this is the case, then the following query should work: 如果是这种情况,则以下查询应该起作用:

     SELECT PersonalInfo.[ID] FROM PersonalInfo ORDER BY CAST(id AS INT) -- for MS Access maybe: ORDER BY CInt(id) 

    And if it does fix the problem then the scheme should probably be fixed! 如果确实解决了问题,则该方案可能应该得到解决!

  2. A sort might be applied after the query, such as within the ComboBox itself! 查询可能会进行排序,例如在ComboBox本身中! Make sure that Sorted is false. 确保Sorted为false。

    [The Sorted] property specifies whether the ComboBox sorts existing entries and add new entries to the appropriate sorted position in the list. [Sorted]属性指定ComboBox是否对现有条目进行排序并将新条目添加到列表中的适当排序位置。 You can use this property to automatically sort items in a ComboBox .. The sort is case-insensitive and in alphabetically ascending order. 您可以使用此属性来自动对ComboBox中的项目进行排序。排序不区分大小写,并且按字母升序排列。

.. or it could be a combination of both! ..或者可能是两者的组合! Also, make sure that the code being run/tested is the latest version and is really being used. 另外,请确保正在运行/测试的代码是最新版本并且确实在使用中。

SELECT PersonalInfo.[ID] FROM PersonalInfo order by ID ASC

将ASC放在选择语句的末尾

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C#-如何重新排列新的HashSet <int> 从最小到最大 - C# - How to rearrange new HashSet<int> by order of least to greatest 按照从最高到最低有效的顺序将 short[] 中的位分开 C# - Separate bits from short[] in order of most to least significant C# 提交表单C#之后,从数据库中检索最新的主键 - Retrieve the most recent primary key from database after form submission C# 从数据库使用EntityFramework C#代码优先-如何在没有主键的情况下映射表 - Using EntityFramework C# code-first from database - how to map table with no primary key 在我根据主键从数据库中过滤后,值变为 Null - C# Winforms - Values Becomes Null after I filter from database on the basis of a primary key - C# Winforms 如何通过文本框(Windows窗体),C#检查我的数据库中是否存在主键 - How to check if the primary key exists in my database from a textbox (windows form), c# C#WPF使用datagrid从数据库中删除主键及其主键 - C# WPF delete item with its primary key from database using datagrid 使用ASP和C#从主键填充外键 - fill foreign key from primary key using asp and C# 显示数据库中的项目(C#) - Displaying items from database (c#) C#从数据库显示图像 - C# Displaying Image from Database
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM