GridViewComboBoxColumn not keeping display value after leaving cell

698 views Asked by At

I currently have the combobox showing the valuemember when the cell is not selected. You click the cell and the displaymember shows to make a selection. After making a selection and clicking off the valuemember does change to the correct number but I want to have it show the displaymember after leaving the cell.

    //class to hold the data
    private class PriorityData
    {
        public string PriorityName { get; set; }
        public string PriorityCode { get; set; }
        public Color PriorityColor { get; set; }
    }

    //Construct the Priority column
    _priorityColumn = new DataGridViewComboBoxColumn
    {
        ValueMember = "PriorityCode",
        DisplayMember = "PriorityName",
        Name = "Priority",
        DataPropertyName = "Priority",
        HeaderText = "Priority",
        SortMode = DataGridViewColumnSortMode.NotSortable
    };

    private void LoadPriorityList()
    {
        try
        {
            using (var sqlConn = new SqlConnection(Program.sqlMgr.ConnectionString))
            {
                const string query = "SELECT * FROM mcPriority";

                using (var da = new SqlDataAdapter(query, sqlConn))
                {
                    var priorityTable = new DataTable();

                    priorityTable.Clear();
                    da.Fill(priorityTable);

                    //Clear the list
                    _priorityList.Clear();

                    //Add the list of shuttels from the database to the shuttle list for the shuttle combo box column
                    foreach (DataRow row in priorityTable.Rows)
                    {
                        _priorityList.Add(new PriorityData() { PriorityName = row["PriorityName"].ToString(), PriorityCode = row["PriorityCode"].ToString(), PriorityColor = Color.FromArgb(row["ColorCode"].ToInt()) });
                    }

                    //Bind the part combo box column to the shuttle list
                    _priorityColumn.DataSource = _priorityList;
                }
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show($"Message: {ex.Message}{Environment.NewLine + Environment.NewLine} Class: {nameof(PaintQueueData)} Method: {nameof(LoadPriorityList)}");
        }
    }

I am able to get the data into the gridview fine all have verified that all the way through to the showing of the screen the value for displaymember is indeed DisplayName.

Just after selecting an option from the dropdown, shows display member.

Just after selecting an option from the dropdown, shows display member.

Then clicking out of the cell and it reverts to the value member.

Then clicking out of the cell and it reverts to the value member.

Any and all help would be greatly appreciated, been stuck on this for awhile.

1

There are 1 answers

0
LacksCode On BEST ANSWER

Issue was my database datatype for PriorityCode was int and I declared it as string in the code causing data validation errors I am thinking and thus defaulting to the gridview value. In any case matching my datatypes in sql to c# did the trick.