积极答复者
winform的DataGridViewComboBoxCell 设置默认值的问题

问题
-
在datagridview 定义了一列为DataGridViewComboBoxCell ,然后放了些东西进去,在通常情况下运行正常,我想在程序中指定某行该列的默认值,
string tml = "GZ";
int irow = 5
DataGridViewComboBoxCell tbcell = (DataGridViewComboBoxCell)lineportGrid.Rows[irow].Cells["tml"];
tbcell.Value = tml;选项值和列是肯定存在的,
但结果弹出错误为
"System.ArgumentException: DataGridviewCoboBoxCell 值无效。
要替换此默认对话框,请处理 DataError 事件。"
答案
-
dear
因为DataGridViewComboBoxCell下拉选单的Items集合是用来显示在DataGridView,若你不是操作Items集合,而是直接操作Value,他就会跟你报错,你可使用DataError事件来忽略这个错误讯习,但你若直接操作的Value属性的结果并不会显示在DataGridView上。
增加Items
int index = 0; DataGridViewRow row = this.dataGridView1.Rows[index]; DataGridViewComboBoxCell cell = row.Cells["column2"] as DataGridViewComboBoxCell; cell.Items.Clear(); if (cell != null) { for (int i = 0; i < 5; i++) { cell.Items.Add("Item " + i.ToString()); } }
修改Items
int index = 0; DataGridViewRow row = this.dataGridView1.Rows[index]; DataGridViewComboBoxCell cell = row.Cells["column2"] as DataGridViewComboBoxCell; cell.Items[0] = "1234";
秘訣無它,唯勤而已 http://www.dotblogs.com.tw/yc421206/- 已标记为答案 scate233 2011年1月18日 7:49
全部回复
-
dear
因为DataGridViewComboBoxCell下拉选单的Items集合是用来显示在DataGridView,若你不是操作Items集合,而是直接操作Value,他就会跟你报错,你可使用DataError事件来忽略这个错误讯习,但你若直接操作的Value属性的结果并不会显示在DataGridView上。
增加Items
int index = 0; DataGridViewRow row = this.dataGridView1.Rows[index]; DataGridViewComboBoxCell cell = row.Cells["column2"] as DataGridViewComboBoxCell; cell.Items.Clear(); if (cell != null) { for (int i = 0; i < 5; i++) { cell.Items.Add("Item " + i.ToString()); } }
修改Items
int index = 0; DataGridViewRow row = this.dataGridView1.Rows[index]; DataGridViewComboBoxCell cell = row.Cells["column2"] as DataGridViewComboBoxCell; cell.Items[0] = "1234";
秘訣無它,唯勤而已 http://www.dotblogs.com.tw/yc421206/- 已标记为答案 scate233 2011年1月18日 7:49