【发布时间】:2012-12-25 19:50:29
【问题描述】:
我有一个组合框,当您按下按钮时会填充:
private void button1_Click(object sender, EventArgs e)
{
Dictionary<string, string> countries = new Dictionary<string, string>();
//Dictionary<string, List<State>> stateList=new Dictionary<string,List<State>>();
HashSet<string> country = new HashSet<string>();
var lines = File.ReadAllLines("CountryState.txt");
foreach (string line in lines)
{
string[] items = line.Split('\t').ToArray();
try
{
countries.Add(items[0], items[1]);
}
catch (Exception ex)
{
//MessageBox.Show(ex.Message);
}
country.Add(items[0]);
if (!stateList.ContainsKey(items[0])) {
stateList.Add(items[0], new List<State>());
}
if (items[2] != "")
{
stateList[items[0]].Add(new State(items[2], items[3]));
}
if(items[2] !=string.Empty && items[3] !=string.Empty)
stateCodes.Add(items[2], items[3]);
}
comboBox1.DataSource = countries.Keys.ToList();
}
comboBox1 数据源最多有 234 个条目,这对我来说似乎并不过分(其他时候我在网上看到过这个讨论,它提到一千左右的条目过多)。
【问题讨论】:
-
看看这一行你的括号怎么不对齐 if(items[2] !=string.Empty && items[3] !=string.Empty) stateCodes.Add(items [2],项目[3]); }
-
看起来您正在阅读该文件中的所有行。文件有多少行?磁盘上文件的大小是多少?
-
正在读取的文件大约有 350 行
-
此外,如果这是可以一次又一次使用的数据,为什么不将值存储在数据库中并将状态代码绑定到下拉或使用 web 服务的代码..
-
输入文件中有多少行导致添加到
countries时抛出异常?