Jay
Jay

Reputation: 31

DataGridViewComboBoxCell setting value manually, value not valid

here is my code:

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

I use it to fill a DataGridViewComboBoxCell like this:

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

all I want to do now is to select a person:

myDataGridViewComboBoxCell.Value = someNumber;

but keep getting "value is not valid"-error. Any Idea why? When I select an Item in my program I can see the right Value (someNumber) so Display and ValueMember are set correctly...

Upvotes: 3

Views: 16240

Answers (4)

Spike
Spike

Reputation: 2027

Rather than adding to DataGridViewComboBoxColumn.Items, you'll need to set DataGridViewComboBoxColumn.DataSource to a List of Person, and set DataGridViewComboBoxColumn.ValueType = typeof(Person).

Should do it. Although given it's been two months since you asked this, your problem may have lost it's sense of urgency.

Upvotes: 11

Sepehr
Sepehr

Reputation: 7

Try To use enum for your persons name

enum person
{ 
 John,
 Andy,
 Sepehr
};

use this enum to add name to you combobox

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

now when you want to set the vale use enum again. tyhis will fix your problem.

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

Remembere to use --> ToString() ! Worked for me ! :)

Upvotes: -5

EndangeringSpecies
EndangeringSpecies

Reputation: 1594

I had a similar problem. As strange as it sounds, I solved it by storing adding only strings to DataGridViewComboBoxCell.Items. Adding objects (with ToString() nicely implemented etc) caused these "Data Error" issues, but adding strings worked fine.

Upvotes: 0

Julien P
Julien P

Reputation: 323

I also had this issue. I temporarily solved it in quite a dirty way.

The Value property of a DataGridViewComboBoxCell MUST be one of the values contained in the Items property (or bound DataSource)

The problem was that, for I dunno which reason, the cell's Items list (or its DataSource when I tried not entering Items manually) was cleared out as soon as the execution entered the DataGridView.CellFormatting event handler for that Cell.

My dirty fix was to handle the DataGridView.DataError event and fill (again) the DataGridViewComboBoxCell.Items at that point.

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: Sorry for the VB.Net answer, if you have problems "translating" it just ask.

Upvotes: 2

Related Questions