Jim*_*ell 5 c# datagridview datagridviewcombobox winforms datagridviewcomboboxcell
我一天中的大部分时间都在为此工作,而解决方案仍然让我望而却步。我的 Winform 应用程序包含DataGridView
其中两列是ComboBox
下拉列表。奇怪的是,DataGridView
似乎填充正确,但在填充时或每当有鼠标悬停或似乎任何其他与DataGridVeiw
. 具体来说,我得到两个重复的错误的System.ArgumentException
和System.FormatException
。这两个错误的消息文本是:
“DataGridViewComboBoxCell 值无效。要替换此默认对话框,请处理 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
}
Run Code Online (Sandbox Code Playgroud)
关于如何解决这个问题的任何想法或建议都非常感谢!
您没有解释 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);
Run Code Online (Sandbox Code Playgroud)
如果您有这些内容的列表,则无需手动将它们一一复制到控件中。只需将 绑定List
到 DGV 即可。对控件中数据的编辑将流向列表:
dgv1.AutoGenerateColumns = false;
// Show Me the RECORDS!!!!
dgv1.DataSource = Records;
Run Code Online (Sandbox Code Playgroud)
您的下一个障碍可能是SysTime
像日期或时间(如果确实如此)一样显示和运行,因为它被定义为字符串。