1.除非 DataGridView 被数据绑定到支持更改通知并允许删除的 IBindingList,否则不能以编程方式移除行
数据源中删除数据,刷新显示控件。
2.System.ArgumentException:DataGridViewComboBoxCell值无效,要替换此默认对话框,请处理DataError事件
需要在事件中处理:
Code
private void OnDataError(object sender, DataGridViewDataErrorEventArgs e)
{
if((e.ColumnIndex ==2||e.ColumnIndex ==3)&& e.Exception is ArgumentException)
{
e.Cancel = true;
}
}
原因:列中存在空数据项,和ComboBox项不匹配。
private void OnDataError(object sender, DataGridViewDataErrorEventArgs e)
{
if((e.ColumnIndex ==2||e.ColumnIndex ==3)&& e.Exception is ArgumentException)
{
e.Cancel = true;
}
}
3.DataGridView 绑定枚举
Code
1 DataGridViewColumn kXBDataGridViewComboBoxColumn = CreateComboBoxWithEnums();
2 kXBDataGridViewComboBoxColumn.DataPropertyName = "KXB";
3 kXBDataGridViewComboBoxColumn.HeaderText = "性别";
4 kXBDataGridViewComboBoxColumn.Name = "kXBDataGridViewComboBoxColumn";
5 kXBDataGridViewComboBoxColumn.ReadOnly = true;
6 kXBDataGridViewComboBoxColumn.Resizable = DataGridViewTriState.True;
7 kXBDataGridViewComboBoxColumn.Width = 60;
8 dataGridView1.Columns.Add(kXBDataGridViewComboBoxColumn);
9
10
11 static DataGridViewComboBoxColumn CreateComboBoxWithEnums()
12 {
13 DataGridViewComboBoxColumn combo = new DataGridViewComboBoxColumn();
14 combo.DisplayStyle = DataGridViewComboBoxDisplayStyle.Nothing;
15 combo.DataSource = Enum.GetValues(typeof(EnumGender));
16 string[] names = Enum.GetNames(typeof(EnumGender));
17 Dictionary<string, int> dictionary = new Dictionary<string, int>();
18 foreach (string name in names)
19 {
20 EnumGender parse = (EnumGender)Enum.Parse(typeof(EnumGender), name);
21 dictionary.Add(EnumDescription.GetEnumDescription(parse), (int)parse);
22 }
23 combo.DataSource = new BindingSource(dictionary, null);
24 combo.DisplayMember = "Key";
25 combo.ValueMember = "Value";
26 return combo;
27 }
4.RowPrePaint 事件,System.ArgumentException: DataGridViewComboBoxCell 值无效
1 DataGridViewColumn kXBDataGridViewComboBoxColumn = CreateComboBoxWithEnums();
2 kXBDataGridViewComboBoxColumn.DataPropertyName = "KXB";
3 kXBDataGridViewComboBoxColumn.HeaderText = "性别";
4 kXBDataGridViewComboBoxColumn.Name = "kXBDataGridViewComboBoxColumn";
5 kXBDataGridViewComboBoxColumn.ReadOnly = true;
6 kXBDataGridViewComboBoxColumn.Resizable = DataGridViewTriState.True;
7 kXBDataGridViewComboBoxColumn.Width = 60;
8 dataGridView1.Columns.Add(kXBDataGridViewComboBoxColumn);
9
10
11 static DataGridViewComboBoxColumn CreateComboBoxWithEnums()
12 {
13 DataGridViewComboBoxColumn combo = new DataGridViewComboBoxColumn();
14 combo.DisplayStyle = DataGridViewComboBoxDisplayStyle.Nothing;
15 combo.DataSource = Enum.GetValues(typeof(EnumGender));
16 string[] names = Enum.GetNames(typeof(EnumGender));
17 Dictionary<string, int> dictionary = new Dictionary<string, int>();
18 foreach (string name in names)
19 {
20 EnumGender parse = (EnumGender)Enum.Parse(typeof(EnumGender), name);
21 dictionary.Add(EnumDescription.GetEnumDescription(parse), (int)parse);
22 }
23 combo.DataSource = new BindingSource(dictionary, null);
24 combo.DisplayMember = "Key";
25 combo.ValueMember = "Value";
26 return combo;
27 }
Code
1 int index = this.dataGridView1.Rows.Add();
2 DataGridViewRow row = this.dataGridView1.Rows[index];
3 DataGridViewComboBoxCell cell = row.Cells[0] as DataGridViewComboBoxCell;
4 if (cell != null)
5 {
6 int tt = 1;
7 cell.Items.Add("string" ( tt).ToString());
8 cell.Items.Add("string" ( tt).ToString());
9 cell.Items.Add("string" ( tt).ToString());
10 cell.Items.Add("string" ( tt).ToString());
11 cell.Items.Add("string" ( tt).ToString());
12 }
1 int index = this.dataGridView1.Rows.Add();
2 DataGridViewRow row = this.dataGridView1.Rows[index];
3 DataGridViewComboBoxCell cell = row.Cells[0] as DataGridViewComboBoxCell;
4 if (cell != null)
5 {
6 int tt = 1;
7 cell.Items.Add("string" ( tt).ToString());
8 cell.Items.Add("string" ( tt).ToString());
9 cell.Items.Add("string" ( tt).ToString());
10 cell.Items.Add("string" ( tt).ToString());
11 cell.Items.Add("string" ( tt).ToString());
12 }
dataGridView1[4, 2].Value = ((DataGridViewComboBoxCell)dataGridView1[4, 2]).Items[0].ToString();
设置第2行第5列的默认值为ComboBox中的第一项(Items[0]),此列为DataGridViewComboBoxCell。 态度决定一切,技术改变生活作者:winzheng
出处: http://cnblogs.com/winzheng
本文为原创作品,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文链接,否则保留追究法律责任的权利。