C# Runtime Error: "DataGridViewComboBoxCell value is not valid"
C# Runtime Error: "DataGridViewComboBoxCell value is not valid"
一天中的大部分时间我都在为此工作,但我仍然找不到解决方案。我的 Winform 应用程序包含一个 DataGridView
,其中两列是 ComboBox
下拉列表。奇怪的是,DataGridView
似乎可以正确填充,但它会在填充时或每当有鼠标悬停或看似与 DataGridVeiw
相关的任何其他可触发事件时抛出大量错误。具体来说,我收到 System.ArgumentException
和 System.FormatException
的两个重复错误。这两个错误的消息文本是:
"DataGridViewComboBoxCell value is not valid. To replace this default dialog please handle the DataError event."
我不想仅仅通过处理 DataError
事件来掩盖这个问题。 我想解决导致错误的问题。这是我填充列表的方式:
class ManageProcsRecord
{
public SectionType PageSection { get; set; }
public Int32 ContentID { get; set; }
public String Content { get; set; }
public Int32 SummaryID { get; set; }
public RoleType UserRole { get; set; }
public String Author { get; set; }
public String SysTime { get; set; }
}
public enum SectionType
{
ESJP_SECTION_HEADER = 1,
ESJP_SECTION_FOOTER,
ESJP_SECTION_BODY
}
public enum RoleType
{
ESJP_ROLE_NONE = 1,
ESJP_ROLE_TEST_ENG,
ESJP_ROLE_FEATURE_LEAD,
ESJP_ROLE_TEAM_LEAD
}
List<ManageProcsRecord> records =
this.dbif.GetProcedure(this.procList.ElementAt(selectedIndex).PrimaryKey);
foreach (ManageProcsRecord record in records)
{
DataGridViewRow row = new DataGridViewRow();
row.CreateCells(this.dataGridView1);
row.Cells[0].ValueType = typeof(SectionType);
row.Cells[0].Value = record.PageSection;
row.Cells[1].Value = record.Content;
row.Cells[2].Value = (record.SummaryID != -1);
row.Cells[3].ValueType = typeof(RoleType);
row.Cells[3].Value = record.UserRole;
row.Cells[4].Value = record.Author;
row.Cells[5].Value = record.SysTime;
this.dataGridView1.Rows.Add(row); // error fest starts here
}
非常感谢任何关于如何解决此问题的想法或建议!
您没有解释 DGV 是如何填充的,但我假设您进行了设计时设置。如果您从这些枚举中键入 names/data,它们将被键入为字符串,因此您会遇到匹配错误。使用枚举填充它并设置类型,它不会对你大喊大叫:
List<ProcRecord> Records = new List<ProcRecord>();
// add some records
Records.Add(new ProcRecord()
{
Author = "Ziggy",
PageSection = SectionType.ESJP_SECTION_BODY,
UserRole = RoleType.ESJP_ROLE_FEATURE_LEAD
});
Records.Add(new ProcRecord()
{
Author = "Zalgo",
PageSection = SectionType.ESJP_SECTION_BODY,
UserRole = RoleType.ESJP_ROLE_FEATURE_LEAD
});
// set cbo datasources
DataGridViewComboBoxColumn col = (DataGridViewComboBoxColumn)dgv1.Columns[0];
col.DataSource = Enum.GetValues(typeof(SectionType));
col.ValueType = typeof(SectionType);
col = (DataGridViewComboBoxColumn)dgv1.Columns[4];
col.DataSource = Enum.GetValues(typeof(RoleType));
col.ValueType = typeof(RoleType);
如果你有那些东西的列表,就没有必要手动将它们一一复制到控件中。只需将 List
绑定到 DGV。对控件中数据的编辑将流向列表:
dgv1.AutoGenerateColumns = false;
// Show Me the RECORDS!!!!
dgv1.DataSource = Records;
您的下一个障碍可能是让 SysTime
显示并像日期或时间(如果是这样的话)一样工作,因为它被定义为字符串。
一天中的大部分时间我都在为此工作,但我仍然找不到解决方案。我的 Winform 应用程序包含一个 DataGridView
,其中两列是 ComboBox
下拉列表。奇怪的是,DataGridView
似乎可以正确填充,但它会在填充时或每当有鼠标悬停或看似与 DataGridVeiw
相关的任何其他可触发事件时抛出大量错误。具体来说,我收到 System.ArgumentException
和 System.FormatException
的两个重复错误。这两个错误的消息文本是:
"DataGridViewComboBoxCell value is not valid. To replace this default dialog please handle the DataError event."
我不想仅仅通过处理 DataError
事件来掩盖这个问题。 我想解决导致错误的问题。这是我填充列表的方式:
class ManageProcsRecord
{
public SectionType PageSection { get; set; }
public Int32 ContentID { get; set; }
public String Content { get; set; }
public Int32 SummaryID { get; set; }
public RoleType UserRole { get; set; }
public String Author { get; set; }
public String SysTime { get; set; }
}
public enum SectionType
{
ESJP_SECTION_HEADER = 1,
ESJP_SECTION_FOOTER,
ESJP_SECTION_BODY
}
public enum RoleType
{
ESJP_ROLE_NONE = 1,
ESJP_ROLE_TEST_ENG,
ESJP_ROLE_FEATURE_LEAD,
ESJP_ROLE_TEAM_LEAD
}
List<ManageProcsRecord> records =
this.dbif.GetProcedure(this.procList.ElementAt(selectedIndex).PrimaryKey);
foreach (ManageProcsRecord record in records)
{
DataGridViewRow row = new DataGridViewRow();
row.CreateCells(this.dataGridView1);
row.Cells[0].ValueType = typeof(SectionType);
row.Cells[0].Value = record.PageSection;
row.Cells[1].Value = record.Content;
row.Cells[2].Value = (record.SummaryID != -1);
row.Cells[3].ValueType = typeof(RoleType);
row.Cells[3].Value = record.UserRole;
row.Cells[4].Value = record.Author;
row.Cells[5].Value = record.SysTime;
this.dataGridView1.Rows.Add(row); // error fest starts here
}
非常感谢任何关于如何解决此问题的想法或建议!
您没有解释 DGV 是如何填充的,但我假设您进行了设计时设置。如果您从这些枚举中键入 names/data,它们将被键入为字符串,因此您会遇到匹配错误。使用枚举填充它并设置类型,它不会对你大喊大叫:
List<ProcRecord> Records = new List<ProcRecord>();
// add some records
Records.Add(new ProcRecord()
{
Author = "Ziggy",
PageSection = SectionType.ESJP_SECTION_BODY,
UserRole = RoleType.ESJP_ROLE_FEATURE_LEAD
});
Records.Add(new ProcRecord()
{
Author = "Zalgo",
PageSection = SectionType.ESJP_SECTION_BODY,
UserRole = RoleType.ESJP_ROLE_FEATURE_LEAD
});
// set cbo datasources
DataGridViewComboBoxColumn col = (DataGridViewComboBoxColumn)dgv1.Columns[0];
col.DataSource = Enum.GetValues(typeof(SectionType));
col.ValueType = typeof(SectionType);
col = (DataGridViewComboBoxColumn)dgv1.Columns[4];
col.DataSource = Enum.GetValues(typeof(RoleType));
col.ValueType = typeof(RoleType);
如果你有那些东西的列表,就没有必要手动将它们一一复制到控件中。只需将 List
绑定到 DGV。对控件中数据的编辑将流向列表:
dgv1.AutoGenerateColumns = false;
// Show Me the RECORDS!!!!
dgv1.DataSource = Records;
您的下一个障碍可能是让 SysTime
显示并像日期或时间(如果是这样的话)一样工作,因为它被定义为字符串。