我有一个DataGridView
在运行时填充了几ComboBoxColumn
列。例如,
var newCountryColumn = new DataGridViewComboBoxColumn();
newCountryColumn.HeaderText = "Country";
newCountryColumn.DataSource = CountryListArray.ToArray();
newCountryColumn.DisplayMember = "Name";
newCountryColumn.ValueMember = "Code";
等等。现在,在运行时,用户选择一个要打开的文件,它被逐行解析成一个数组。
var lines = File.ReadAllLines(path + "\\" + choosenFile);
foreach (string line in lines) {
numOfRecords++;
errorCounter = 0;
string[] items = line.Split('\t').ToArray();
int billState = headerIndex[0] - 1;
int billCountry = headerIndex[1] - 1;
int shipState = headerIndex[2] - 1;
int shipCountry = headerIndex[3] - 1;
for (int i = 0; i < headerIndex.Count; i++) {
int index = headerIndex[i];
/*Get the state and country codes from the files using the correct indices*/
Globals.Code = items[index - 1].ToUpper();
//If the code can't be found in either list
if (!CountryList.ContainsKey(Globals.Code) && !StateList.ContainsKey(Globals.Code)) {
errorCounter++;
if (errorCounter == 1){
dataGridView1.Rows.Add(items);
}
}
}
}
现在,这很好用,除非我在 , 中滚动DataGridView
到组合框所在的位置。显然,代码不喜欢将 items 数组中的值添加到预先存在的组合框列中。我得到一个错误对话框:
DataGridView 发生以下异常: System.ArguementException: DataGridViewComboBoxCell 值无效。
项目数组中的项目可以显示在组合框列中吗?