简体   繁体   中英

C# Runtime Error: “DataGridViewComboBoxCell value is not valid”

I've been working most of the day on this, and the solution continues to elude me. My Winform application contains a DataGridView wherein two of the columns are ComboBox dropdown lists. Oddly, the DataGridView appears to populate correctly, but it throws up a plethora of errors while populating or whenever there is a mouseover or seemingly any other triggerable event related to the DataGridVeiw . Specifically, I am getting two repeating errors for System.ArgumentException and System.FormatException . The message text for both of these errors is:

"DataGridViewComboBoxCell value is not valid. To replace this default dialog please handle the DataError event."

I don't want to merely mask this problem by handling the DataError event. I want to resolve the issue that is causing error. This is how I am populating the list:

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
}

Any thoughts or suggestions on how to resolve this are much appreciatd!

You didnt explain how the DGV got populated, but I will assume you did a design time setup. If you typed in the names/data from those Enums, they will be typed as strings so you get a match error. Populate it using the Enum and set the Type as such and it wont yell at you:

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

If you have a list of those things, there is no need to manually copy them one by one into the control. Just bind the List to the DGV. Edits to the data in the control will flow thru to the List:

dgv1.AutoGenerateColumns = false;
// Show Me the RECORDS!!!!
dgv1.DataSource = Records;

在此处输入图片说明

Your next obstacle is likely to be getting SysTime to display and act like a Date or Time (if that is what it is) since it is defined as string.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM