Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm Data Binding / November 2005

Tip: Looking for answers? Try searching our database.

DataGridViewComboBoxColumn where ValueMember is bind to property which type is Guid = ERROR

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Oka Morikawa - 17 Nov 2005 22:34 GMT
I have Custom Collection with Custom Object which has Guid as Id - I bind
DataGridView into it and it works BUT if I make one Column as
DataGridViewComboBoxColumn the trouble becomes.

If the DataGridViewComboBoxColumn ValueMember property type is Guid I get
nice error when I try to insert a new row in DataGridView.

The following exception occurred in the DataGridView:
System.ArgumentException: DataGridViewComboBoxCell value is not valid.
To replace this default dialog please handle the DataError event.

If I suppress this event with custom DataError event I see the problem,
DataGridView cannot insert selected value from the
DataGridViewComboBoxColumn at all. BUT this problem goes a way if
DataGridViewComboBoxColumn ValueMember bind property type is String... ok,
no Guid but String is okay - problem is that I would like to have Id's as
Guid on my custom objects or I need to make those String and add logic on
properties to check that the String is actually valid Guid... not good.

Also if I try this with DataSet / DataTable I don't have any problems even
that DataGridViewComboBoxColumn ValueMember bind property type is Guid, Int,
String or what ever... so is this a bug (don't think so), or (more likely)
I'm missing some properties / methods from interfaces I need to implement to
make this work?

Regards,
Oka Morikawa
D I G I N E
Bart Mermuys - 18 Nov 2005 01:31 GMT
Hi,

>I have Custom Collection with Custom Object which has Guid as Id - I bind
>DataGridView into it and it works BUT if I make one Column as
[quoted text clipped - 20 lines]
> likely) I'm missing some properties / methods from interfaces I need to
> implement to make this work?

A string is a reference type while a Guid is a value type.  When a property
type is a reference type then it can return null, when it is a value type it
can't return null, at the best it can return the default initialized value.

So if the grid (indireclty) adds a new custom object, then your key (Guid)
isn't null but {0000000-000....} instead and because that guid doesn't
exists in the ComboBox it crashes.

Untyped DataTable's don't have this problem since all property types are
object, and so they can return DBNull.Value or a boxed Guid.

In VS2005 final release you can use the new nullable type support & syntax:

public class CustomObject
{
 private Guid? someKey = null;

 // now this property can return null (eg. as default for new objects) or a
Guid
 public Guid? SomeKey
 {
   get { return someKey; }
   set  { someKey = value; }
 }
}

(Note: This doesn't work with VC2005beta2, because of a boxing problem with
nullable types)

--- Another thing you can do, is make sure that newly created objects have a
Guid that exists in the ComboBox.

HTH,
Greetings

> Regards,
> Oka Morikawa
> D I G I N E
Oka Morikawa - 18 Nov 2005 10:20 GMT
Hi, I tried that and no go. Behaviour is little different thought, now I can
select items from ComboBox (before it gave the exception immediately I
insert new row into grid) but it cannot store the Id into that column and
gives the same exception when I select item from ComboBox and go to next
column.

I think that ComboBox tries to insert that Id value as String NOT Guid and
that's why it gives the exception? Any ideas to get workaround for this?

I'm runnin VS2005RTM

Regards,
Oka Morikawa
D I G I N E

> Hi,
>
[quoted text clipped - 64 lines]
>> Oka Morikawa
>> D I G I N E
Bart Mermuys - 18 Nov 2005 14:07 GMT
Hi,

> Hi, I tried that and no go. Behaviour is little different thought, now I
> can select items from ComboBox (before it gave the exception immediately I
[quoted text clipped - 4 lines]
> I think that ComboBox tries to insert that Id value as String NOT Guid and
> that's why it gives the exception? Any ideas to get workaround for this?

I assume i tried something in the lines of what you are doing and it worked
(though with nullable types).  The editing worked too.  Without seeing any
code i have no idea why it doesn't work for you.  If you want you can post
all relevant code.

HTH,
Greetings

> I'm runnin VS2005RTM
>
[quoted text clipped - 70 lines]
>>> Oka Morikawa
>>> D I G I N E
Oka Morikawa - 18 Nov 2005 23:59 GMT
Thanks, I tested also with bare minimum Custom Objects and it works. My
Custom Objects inherits Base Objects where is some common logic and this
seems to break something...

I'll post later more about this - Thanks!

Regards,
Oka Morikawa
D I G I N E
> Hi,
>
[quoted text clipped - 90 lines]
>>>> Oka Morikawa
>>>> D I G I N E
Oka Morikawa - 21 Nov 2005 12:38 GMT
I found the bug and it's quite weird. I don't know how the DataGridView
works internally but the problem was the custom objects list base object. I
inherit all my custom collections from base class and the problem was in
FindCore method:

       protected override bool SupportsSearchingCore
       {
           get { return true; }
       }

       protected override int FindCore(PropertyDescriptor prop, object key)
       {
           int result = -1;

           for (int i = 0; i < this.Count; i++)
           {
               object value = prop.GetValue(this.Items[i]);

               if (value.ToString() == key.ToString())
                   result = i;
           }

           return result;
       }

the bug was in line: if (value.ToString() == key.ToString()) where it was
originally without .ToString() method.
Question is why DataGridView runs this FindCore method even that there is no
such function going on in DGV? Also why DGV crash on that particular line
since both value and key are object types what I'm comparing? Then I'm
wondering if all this FindCore method will work with all types (object value
= prop.GetValue(this.Items[i]);) I throw in or do I soon run into new bug?
...now it works but I'm afraid of my code since I don't know what I fixed
and why.

Regards,
Oka Morikawa
D I G I N E

> Hi,
>
[quoted text clipped - 90 lines]
>>>> Oka Morikawa
>>>> D I G I N E
Bart Mermuys - 21 Nov 2005 14:04 GMT
Hi,

>I found the bug and it's quite weird. I don't know how the DataGridView
>works internally but the problem was the custom objects list base object. I
[quoted text clipped - 31 lines]
> new bug? ...now it works but I'm afraid of my code since I don't know what
> I fixed and why.

I don't think it is that strange it calls FindCore, at least not for the
Custom List you have bound to the ComboBox.  It uses that to find the right
item based on the key in the datagrid.

I think it originates from the following problem :
int i = 10;
object o = i;
object p = i;
Console.WriteLine( o == p );  //writes false

It writes false, because each value type is boxed and has its own reference
and therefore reference comparison fails.
To be safe you should use:
....
 if ( object.Equals(value, key) )
     result = i;
....

HTH,
Greetings

> Regards,
> Oka Morikawa
[quoted text clipped - 94 lines]
>>>>> Oka Morikawa
>>>>> D I G I N E
Jim Wooley - 21 Nov 2005 19:27 GMT
I just ran into this one as well. My fix was to remove the binding for the
datagrid's datasource in the dispose. If you try to dispose the combobox
column's datasource before unbinding it (which is what the
components.dispose method is likely doing in your case), you will get that
error. The below code fixed the problem for me.

'UserControl overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
'Following line is required to dispose of the comboboxcolumn properly
DataGridView1.DataSource = Nothing
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub

Jim Wooley
www.devauthority.com/blogs/jwooley

>I have Custom Collection with Custom Object which has Guid as Id - I bind
>DataGridView into it and it works BUT if I make one Column as
[quoted text clipped - 24 lines]
> Oka Morikawa
> D I G I N E

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.