【发布时间】:2020-07-15 10:01:54
【问题描述】:
目标:
从 MySQL 中获取所有文件目录并将它们放入字典中。
将它们作为文件名显示在组合框中。例如
filename将组合框值指定为完整目录。例如
c:\users\user\desktop\filename.jpg
代码:
string filenames = "select filename from request_label_signoff where progress_user1 is null or progress_user2 is null";
//On load - load specific images from query above
private void Form15_Load(object sender, EventArgs e)
{
//Dict to store file into
Dictionary<string, string> files = new Dictionary<string, string>();
using (var conn = new MySqlConnection(connString))
{
conn.Open();
using (var cmd = new MySqlCommand(filenames, conn))
{
using (MySqlDataReader reader = cmd.ExecuteReader())
{
while (reader.Read())
{
//add filename without extension and full directory
files.Add(Path.GetFileNameWithoutExtension(reader.GetString(0)), reader.GetString(0));
}
}
}
}
comboBox1.DataSource = new BindingSource(files, null);
comboBox1.DisplayMember = "Key";
comboBox1.ValueMember = "Value";
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
value = ((KeyValuePair<string, string>)comboBox1.SelectedItem).Value;
pictureBox1.Image = Image.FromFile(value);
}
问题:
由于某种原因,组合框的显示值如下所示:
文本输出:[abc 123, C:\Users...]
而它应该是abc 123,旁边没有目录。
问题:
为什么他的组合框显示值同时显示两个项目?
【问题讨论】:
-
您似乎以错误的方式填充组合框。谷歌了解如何填充组合框或使用此 SO 问题作为参考:stackoverflow.com/questions/7423911/…
标签: c#