gpt4 book ai didi

C# 运行时错误 : "DataGridViewComboBoxCell value is not valid"

转载 作者:行者123 更新时间:2023-12-03 16:42:37 28 4
gpt4 key购买 nike

我一天中的大部分时间都在为此工作,但解决方案仍然让我难以捉摸。我的 Winform 应用程序包含一个 DataGridView其中两列是ComboBox下拉列表。奇怪的是,DataGridView似乎填充正确,但在填充时或每当鼠标悬停或看似与 DataGridVeiw 相关的任何其他可触发事件时都会引发大量错误。 .具体来说,我收到两个重复的错误 System.ArgumentExceptionSystem.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 是如何填充的,但我假设您进行了设计时设置。如果您从这些枚举中输入名称/数据,它们将被输入为字符串,因此您会收到匹配错误。使用 Enum 填充它并将 Type 设置为这样,它不会对你大喊大叫:

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

如果你有这些东西的列表,就不需要手动将它们一一复制到控件中。只需绑定(bind) List到 DGV。对控件中数据的编辑将流向列表:
dgv1.AutoGenerateColumns = false;
// Show Me the RECORDS!!!!
dgv1.DataSource = Records;

enter image description here

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

关于C# 运行时错误 : "DataGridViewComboBoxCell value is not valid",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37846950/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com