【问题标题】:DataGridViewComboBoxCell setting value manually, value not validDataGridViewComboBoxCell 手动设置值,值无效
【发布时间】:2010-05-02 14:02:39
【问题描述】:

这是我的代码:

        private class Person
        {
            private string myName;
            private int myValue;

            public Person(string name, int value)
            {
                myName = name; 
                myValue = value;
            }
            public override string ToString()
            {
                return myName;
            }

            public string Name
            {
                get { return myName; }
                set { myName = value; }
            }

            public int Value
            {
                get { return myValue; }
                set { myValue = value; }
            }
        }

我用它来填充 DataGridViewComboBoxCell,如下所示:

myDataGridViewComboBoxCell.ValueMember = "Value";
myDataGridViewComboBoxCell.DisplayMember = "Name";
myDataGridViewComboBoxCell.Items.Add(new Person("blabla", someNumber));

我现在要做的就是选择一个人:

myDataGridViewComboBoxCell.Value = someNumber;

但不断收到“值无效”-错误。任何想法为什么? 当我在我的程序中选择一个项目时,我可以看到正确的值 (someNumber),因此 Display 和 ValueMember 设置正确......

【问题讨论】:

    标签: c# datagridview


    【解决方案1】:

    您需要将DataGridViewComboBoxColumn.DataSource 设置为人员列表,并设置DataGridViewComboBoxColumn.ValueType = typeof(Person),而不是添加到DataGridViewComboBoxColumn.Items

    应该这样做。虽然你问这个问题已经两个月了,但你的问题可能已经失去了紧迫感。

    【讨论】:

    • 我在DataGridView 上看不到ValueType 的属性。你是说DataGridViewComboBoxColumn 吗?
    • 是的,它在DataGridViewComboBoxColumn
    【解决方案2】:

    我也有这个问题。我暂时以相当肮脏的方式解决了它。

    DataGridViewComboBoxCell 的 Value 属性必须是 Items 属性(或绑定的 DataSource)中包含的值之一

    问题在于,由于我不知道是什么原因,当执行进入该单元的DataGridView.CellFormatting 事件处理程序时,单元的项目列表(或当我尝试不手动输入项目时它的数据源)被清除。

    我的肮脏解决方法是处理 DataGridView.DataError 事件并在那时(再次)填充 DataGridViewComboBoxCell.Items。

    Private Sub myDataGridView_DataError(ByVal sender As System.Object, _ 
                    ByVal e As System.Windows.Forms.DataGridViewDataErrorEventArgs) _
                    Handles dgvServicesToTransfer.DataError
    
            'Dirty Fix 
            If e.ColumnIndex = myComboBoxColumn.Index Then
                Dim row As DataGridViewRow = dgvServicesToTransfer.Rows(e.RowIndex)
                ' Fill in your DataGridViewComboBoxCell's Items here:
                ' ...
                ' ...
                If allWentWell Then
                    ' Cancel the exception
                    e.ThrowException = False
                End If
            End If
    
        End Sub
    End Class
    

    PS:对于 VB.Net 的回答很抱歉,如果您在“翻译”时遇到问题,请询问。

    【讨论】:

    • 这也吸引了我。我将 display 和 value 成员都与 valuetype 一起设置,但仍然有问题。找出当前下拉列表中不存在的值帮助我找到了根本原因。
    【解决方案3】:

    我遇到了类似的问题。听起来很奇怪,我通过仅将字符串添加到 DataGridViewComboBoxCell.Items 来解决它。添加对象(很好地实现了 ToString() 等)会导致这些“数据错误”问题,但添加字符串效果很好。

    【讨论】:

      【解决方案4】:

      尝试使用枚举作为您的人名

      enum person
      { 
       John,
       Andy,
       Sepehr
      };
      

      使用此枚举为您的组合框添加名称

      myDataGridViewComboBoxCell.Items.Add ( person.andy.tostring(),number );
      

      现在,当您想再次设置 vale 时,请使用枚举。 tyhi 会解决你的问题。

      myDataGridViewComboBoxCell.Value = person.Andy.ToString() ;
      

      记得使用 --> ToString() ! 为我工作! :)

      【讨论】:

      • 枚举名称?这使得你的系统非常不灵活,当一个新人加入时会发生什么,你必须重新编译代码。
      • 对人名使用枚举可能是我在 StackOverflow 上见过的最糟糕的建议。
      猜你喜欢
      • 2014-07-21
      • 1970-01-01
      • 2010-10-13
      • 1970-01-01
      • 1970-01-01
      • 2016-10-17
      • 1970-01-01
      相关资源
      最近更新 更多