离开单元格后GridViewComboBoxColumn不保持显示值

时间:2016-03-08 21:46:38

标签: c# winforms gridview display valuemember

我目前让组合框显示未选择单元格时的值成员。单击单元格,显示成员显示以进行选择。在做出选择并单击关闭后,值成员会更改为正确的数字,但我希望在离开单元格后显示该成员。

    //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)}");
        }
    }

我能够将数据输入gridview,所有已经验证的一直到显示屏幕的显示成员的值确实是DisplayName。

从下拉列表中选择一个选项后,显示显示成员。

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

然后单击单元格,它将恢复为值成员。

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

任何和所有的帮助将不胜感激,一直坚持这一点。

1 个答案:

答案 0 :(得分:0)

问题是我的PriorityCode的数据库数据类型是int,我在代码中将其声明为字符串,导致数据验证错误,我正在思考,因此默认为gridview值。在任何情况下,将我在sql中的数据类型与c#相匹配都可以解决问题。