Solved

DataGridViewComboBoxCell with Structured Data

Posted on 2010-08-13
13
481 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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 

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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

762 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

17 Experts available now in Live!

Get 1:1 Help Now