Solved

DataGridViewComboBoxCell with Structured Data

Posted on 2010-08-13
13
484 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
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 
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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

803 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