Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 490
  • Last Modified:

DataGridViewComboBoxCell with Structured Data

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
diablo089
Asked:
diablo089
  • 7
  • 6
1 Solution
 
John (Yiannis) ToutountzoglouCommented:
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
 
John (Yiannis) ToutountzoglouCommented:
0
 
diablo089Author Commented:
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
John (Yiannis) ToutountzoglouCommented:
so something wrong happens with your data...
can you please posrt me how youa re populating the datagridview and the datagridviewcombobox?
0
 
diablo089Author Commented:
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
 
John (Yiannis) ToutountzoglouCommented:
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
 
diablo089Author Commented:
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
 
John (Yiannis) ToutountzoglouCommented:
if your combobxes are databound i do not thing that you are able to add items in ...are both comboboxes databound?
0
 
John (Yiannis) ToutountzoglouCommented:
You can do that only with an insert sql statemnt to the table that your combobox is bounded...
0
 
diablo089Author Commented:
No, they aren't databound.
0
 
diablo089Author Commented:
I add all items to the list manually in the combobox at runtime based on what is entered in another column.
0
 
John (Yiannis) ToutountzoglouCommented:
that changes.........
Where your are using :
If Fm.Depth <> 1.0E+30 Then
     cbTopsCell.Items.Add(Fm)
     cbBottomsCell.Items.Add(Fm)
End If
0
 
diablo089Author Commented:
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

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 7
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now