Solved

DataGridViewComboBoxCell with Structured Data

Posted on 2010-08-13
13
482 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

861 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now