void Fillcombo()
{
cbxProducts.Text = "";
cbxProducts.Items.Clear();
string constring = "Data Source = (LocalDB)\\MSSQLLocalDB; AttachDbFilename = \"C:\\Users\\hannes.corbett\\Desktop\\Barcode Scanning\\Barcode Scanning\\BarcodeDB.mdf\"; Integrated Security = True";
string Query = "SELECT Name FROM Products;";
SqlConnection conDataBase = new SqlConnection(constring);
SqlCommand cmdDataBase = new SqlCommand(Query, conDataBase);
SqlDataReader myReader;
try
{
conDataBase.Open();
myReader = cmdDataBase.ExecuteReader();
while (myReader.Read())
{
string sName = myReader.GetString(myReader.GetOrdinal("Name"));
cbxProducts.Items.Add(sName);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
数据库是什么样的
I can't figure out how to sort the items listed in the combobox by their date while just displaying their Name
我无法弄清楚如何在显示名称时按日期对组合框中列出的项目进行排序
3 个解决方案
#1
2
void Fillcombo()
{
cbxProducts.Text = "";
cbxProducts.Items.Clear();
string constring = "Data Source = (LocalDB)\\MSSQLLocalDB; AttachDbFilename = \"C:\\Users\\hannes.corbett\\Desktop\\Barcode Scanning\\Barcode Scanning\\BarcodeDB.mdf\"; Integrated Security = True";
string Query = "SELECT Name FROM Products ORDER BY EDate;";
SqlConnection conDataBase = new SqlConnection(constring);
SqlCommand cmdDataBase = new SqlCommand(Query, conDataBase);
SqlDataReader myReader;
try
{
conDataBase.Open();
myReader = cmdDataBase.ExecuteReader();
while (myReader.Read())
{
string sName = myReader.GetString(myReader.GetOrdinal("Name"));
cbxProducts.Items.Add(sName);
cbxProducts.Sorted = false;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
This fixed it for me
这为我修好了
#2
0
Sort the dates before adding them to the combobox. Using "ORDER BY", the content is sorted by the String representation.
在将日期添加到组合框之前对日期进行排序。使用“ORDER BY”,内容按字符串表示排序。
Make sure to turn of auto sorting.
确保自动排序。
#3
0
public class YourObject : IComparable{
public YourObject(string name) {
Name = name;
}
public string Name { get; set; }
public override string ToString()
{
return GetHashCode().ToString() + "_" + Name;
}
#region IComparable Members
public int CompareTo(Object other)
{
return Comparer.Default.Compare(this.ToString(), other.ToString());
}
#endregion
}
#1
2
void Fillcombo()
{
cbxProducts.Text = "";
cbxProducts.Items.Clear();
string constring = "Data Source = (LocalDB)\\MSSQLLocalDB; AttachDbFilename = \"C:\\Users\\hannes.corbett\\Desktop\\Barcode Scanning\\Barcode Scanning\\BarcodeDB.mdf\"; Integrated Security = True";
string Query = "SELECT Name FROM Products ORDER BY EDate;";
SqlConnection conDataBase = new SqlConnection(constring);
SqlCommand cmdDataBase = new SqlCommand(Query, conDataBase);
SqlDataReader myReader;
try
{
conDataBase.Open();
myReader = cmdDataBase.ExecuteReader();
while (myReader.Read())
{
string sName = myReader.GetString(myReader.GetOrdinal("Name"));
cbxProducts.Items.Add(sName);
cbxProducts.Sorted = false;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
This fixed it for me
这为我修好了
#2
0
Sort the dates before adding them to the combobox. Using "ORDER BY", the content is sorted by the String representation.
在将日期添加到组合框之前对日期进行排序。使用“ORDER BY”,内容按字符串表示排序。
Make sure to turn of auto sorting.
确保自动排序。
#3
0
public class YourObject : IComparable{
public YourObject(string name) {
Name = name;
}
public string Name { get; set; }
public override string ToString()
{
return GetHashCode().ToString() + "_" + Name;
}
#region IComparable Members
public int CompareTo(Object other)
{
return Comparer.Default.Compare(this.ToString(), other.ToString());
}
#endregion
}