Solved

DataGridViewComboBoxCell with Structured Data

Posted on 2010-08-13
13
486 Views
Last Modified: 2012-05-10
I'd like to use a DataGridViewComboBoxCell with data that's a structure. I've overridden the ToString() method of the structure so that a real value appears in the list of items for my DataGridViewComboBox. Whenever I select an item from the list, I get this error:

System.ArgumentException: DataGridViewComboBoxCell value is not valid.

Is there some way I can override the value for the DataGridViewComboBoxCell so that the value that is displayed via my overridden ToString() method is what is used for the value? Or some other fix?
0
Comment
Question by:diablo089
  • 7
  • 6
13 Comments
 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
ID: 33433194
use initially  the data error event just to see if it  your project is working ...it is a trick !

rivate Sub DataGridView_DataError(ByVal sender As Object, ByVal e As DataGridViewDataErrorEventArgs)
      
            e.ThrowException = false
      
    End Sub

Open in new window

0
 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
ID: 33433267
0
 

Author Comment

by:diablo089
ID: 33433343
Hmm... when I handle DataError, it won't let me select any other value than the first item in the list. Selecting anything else just defaults back to the first item.
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
ID: 33433363
so something wrong happens with your data...
can you please posrt me how youa re populating the datagridview and the datagridviewcombobox?
0
 

Author Comment

by:diablo089
ID: 33433498
If Fm.Depth <> 1.0E+30 Then
     cbTopsCell.Items.Add(Fm)
     cbBottomsCell.Items.Add(Fm)
End If

1.0E+30 is just the null value in my data set, so if the Depth value of the structure Fm is not null, then add the structure to the items list. cbTopsCell and cbBottomsCell are both DataGridViewComboBoxCell's.

The reasoning behind adding the structure is that it has other members that I'd like to use for comparisons/data checks in my program. I'd like to be able to access those other members when a certain item(structure) is selected from the list.
0
 
LVL 18

Accepted Solution

by:
John (Yiannis) Toutountzoglou earned 250 total points
ID: 33433517
before try anything else can please try this?
 Private Sub DataGridView1_DataError(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewDataErrorEventArgs) Handles DataGridView1.DataError
        If e.Context = DataGridViewDataErrorContexts.Formatting Or e.Context = DataGridViewDataErrorContexts.PreferredSize Then
            e.ThrowException = False
        End If
    End Sub

Open in new window

0
 

Author Comment

by:diablo089
ID: 33433568
This fixes the error, but my combobox values still default to the first item in the list. I debugged the program, and if I select anything other than the first item in the list, it uses the selected value at runtime, but the value displayed by my combobox once I select any other column displays only the name of the first item in the list. It doesn't display the value of the item I selected.
0
 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
ID: 33433578
if your combobxes are databound i do not thing that you are able to add items in ...are both comboboxes databound?
0
 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
ID: 33433601
You can do that only with an insert sql statemnt to the table that your combobox is bounded...
0
 

Author Comment

by:diablo089
ID: 33433604
No, they aren't databound.
0
 

Author Comment

by:diablo089
ID: 33433612
I add all items to the list manually in the combobox at runtime based on what is entered in another column.
0
 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
ID: 33433644
that changes.........
Where your are using :
If Fm.Depth <> 1.0E+30 Then
     cbTopsCell.Items.Add(Fm)
     cbBottomsCell.Items.Add(Fm)
End If
0
 

Author Comment

by:diablo089
ID: 33433658
Here's my structure:

<StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Ansi, Pack:=1)> _
    Structure TFm
        <VBFixedString(31), MarshalAs(UnmanagedType.ByValTStr, SizeConst:=31)> Public Name As String
        <VBFixedString(61), MarshalAs(UnmanagedType.ByValTStr, SizeConst:=61)> Public Desc As String
        <VBFixedString(9), MarshalAs(UnmanagedType.ByValTStr, SizeConst:=9)> Public source As String
        Dim Depth As Double '{ Z }
        Dim Symbol As Integer
        <VBFixedString(9), MarshalAs(UnmanagedType.ByValTStr, SizeConst:=9)> Public Quality As String
        <VBFixedString(4097), MarshalAs(UnmanagedType.ByValTStr, SizeConst:=4097)> Public Remarks As String

        'Override ToString Method
        Public Overrides Function ToString() As String
            Return String.Format("{0}", Name)
        End Function
    End Structure

The combobox list gets populated with the Name member of each structure added to the list. So for example, if I have 5 names in the list, and I select Name#3, once I move to a different column, for some reason the value displayed in my combobox is Name#1.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question