按int值排序comboBox

时间:2014-03-13 07:26:36

标签: c# winforms combobox

通过价值订购我的组合框有很多问题任何帮助是非常适用的

   private void Form1_Load(object sender, EventArgs e)
    {
        //text to hold the conbo box, text is grabed from the AS2W14data.csv file from c:\temp\...
        String variable;
        variable = "";
        //filll in the combo box , create a reader
        System.IO.StreamReader sr = System.IO.File.OpenText(@"c:\temp\AS2W14data.csv");
        //use a while loop to read the entire file line by line, using the current line to populate the comboBox
        while (!sr.EndOfStream)
        {
            variable = sr.ReadLine();
            string[] currentLineIndex = variable.Split(',');
            //customer ID is indexed at the string array postion 1
            //Customer name is indexed at the string array position 0
            cboCustomer.Items.Add(currentLineIndex[1].Trim() + " " + currentLineIndex[0].Trim());
        }
        //close the file to prevent errors...
        sr.Close();
    }

这是我的代码到目前为止,我似乎找不到订购它的方法..帮助

2 个答案:

答案 0 :(得分:1)

我建议在将它们添加到组合之前对它们进行排序。这里也是阅读文本文件的一个小捷径。首先,您希望通过将行(ReadLines()返回IEnumerable<string>)流式传输到投影(Select())来读取行,您可以在其中创建具有两个属性的匿名对象 - Id和{ {1}}。最后,您可以通过Name订购这些匿名对象的集合。

Id

答案 1 :(得分:0)

尝试此代码希望它可以使用Sorted属性ComboBox

private void Form1_Load(object sender, EventArgs e)
{
    //text to hold the conbo box, text is grabed from the AS2W14data.csv file from c:\temp\...
    String variable;
    variable = "";
    ArrayList Indexs = new ArrayList();
    //filll in the combo box , create a reader
    System.IO.StreamReader sr = System.IO.File.OpenText(@"c:\temp\AS2W14data.csv");
    //use a while loop to read the entire file line by line, using the current line to populate the comboBox
    while (!sr.EndOfStream)
    {
        variable = sr.ReadLine();
        string[] currentLineIndex = variable.Split(',');
        //customer ID is indexed at the string array postion 1
        //Customer name is indexed at the string array position 0
        Indexs.Add(new AddIndexValues(currentLineIndex[1].Trim() + " " + currentLineIndex[0].Trim());
    }
    //close the file to prevent errors...
    cboCustomer.DataSource = DataBaseBuilds.Indexs;
    sr.Close();
}

public class AddIndexValues
{
   private int i_index;
   public AddIndexValues(int Index)
   {
      i_index = Index;
   }
   public int Index
   {
      get { return i_index; }
   }
}