C# C“运行时错误:”;DataGridViewComboxCell值无效";

C# C“运行时错误:”;DataGridViewComboxCell值无效";,c#,winforms,datagridview,datagridviewcombobox,datagridviewcomboboxcell,C#,Winforms,Datagridview,Datagridviewcombobox,Datagridviewcomboboxcell,我一天大部分时间都在做这件事,但解决办法一直没有找到。我的Winform应用程序包含一个DataGridView,其中两列是ComboBox下拉列表。奇怪的是,DataGridView似乎填充正确,但在填充时或当鼠标悬停或出现与DataGridVeiw相关的任何其他可触发事件时,它会抛出大量错误。具体地说,对于System.ArgumentException和System.FormatException,我得到了两个重复的错误。这两个错误的消息文本为: “DataGridViewComboxCe

我一天大部分时间都在做这件事,但解决办法一直没有找到。我的Winform应用程序包含一个
DataGridView
,其中两列是
ComboBox
下拉列表。奇怪的是,
DataGridView
似乎填充正确,但在填充时或当鼠标悬停或出现与
DataGridVeiw
相关的任何其他可触发事件时,它会抛出大量错误。具体地说,对于
System.ArgumentException
System.FormatException
,我得到了两个重复的错误。这两个错误的消息文本为:

“DataGridViewComboxCell值无效。若要替换此默认对话框,请处理DataError事件。”

我不想仅仅通过处理
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
}
类管理程序记录
{
公共节类型PageSection{get;set;}
公共Int32 ContentID{get;set;}
公共字符串内容{get;set;}
公共Int32 SummaryID{get;set;}
公共角色类型UserRole{get;set;}
公共字符串作者{get;set;}
公共字符串SysTime{get;set;}
}
公共枚举节类型
{
ESJP_段_头=1,
ESJP_章节_页脚,
ESJP_部门_机构
}
公共枚举角色类型
{
ESJP_角色_无=1,
ESJP(角色)测试(英语),
ESJP角色、功能、领导、,
ESJP角色团队领导
}
列出记录=
this.dbif.GetProcedure(this.procList.ElementAt(selectedIndex.PrimaryKey));
foreach(记录中的ManageProcsRecord记录)
{
DataGridViewRow行=新建DataGridViewRow();
row.CreateCells(this.dataGridView1);
行。单元格[0]。ValueType=typeof(SectionType);
行。单元格[0]。值=record.PageSection;
行。单元格[1]。值=记录。内容;
行。单元格[2]。值=(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从这里开始
}

任何关于如何解决这一问题的想法或建议都将不胜感激

您没有解释DGV是如何填充的,但我假设您进行了设计时设置。如果您输入了这些枚举中的名称/数据,它们将被输入为字符串,因此您会得到一个匹配错误。使用枚举填充它,并将类型设置为这样,它不会对您大喊大叫:

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);


您的下一个障碍可能是让
SysTime
显示日期或时间(如果是这样的话),因为它被定义为字符串。

成功了,谢谢!我认为问题在于如何设置列
DataSource
dgv1.AutoGenerateColumns = false;
// Show Me the RECORDS!!!!
dgv1.DataSource = Records;