Solved

Problem with DataGridViewComboBoxCell and Structured Data

Posted on 2010-08-16
5
342 Views
Last Modified: 2012-05-10
I'm using a DataGridViewComboBoxCell in my program, and I'm trying to fill it with choices that are structs. The problem I'm facing is that whenever I select an item in the list, no matter what I choose, the name displayed in the cell defaults to the first item in the list. I've debugged my program, and it's actually using the correct information for what I select, but the cell just displays the wrong name. It will only ever show the first name in the list no matter what I pick.

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 Name
        End Function
    End Structure

Open in new window


I want to see a list of the Name members of my struct data, so as you can see, I've overridden the ToString method for the struct to show the name. I've added items to the list like so:

If Fm.Depth <> 1.0E+30 Then
     cbTopsCell.Items.Add(Fm)
     cbBottomsCell.Items.Add(Fm)
End If

Open in new window


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 later in my program. I'd like to be able to access those other members when a certain item(structure) is selected from the list.

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
Comment
Question by:diablo089
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 24

Accepted Solution

by:
alexey_gusev earned 500 total points
ID: 33452620
have you set DisplayMember and ValueMember for the combobox?
0
 

Author Comment

by:diablo089
ID: 33454540
I tried this to see if I could get any results:

If e.RowIndex >= 0 AndAlso e.ColumnIndex = 3 Then
      Dim cbTopsCell As DataGridViewComboBoxCell = DataGridView1.Rows(e.RowIndex).Cells("Top")
      i = 2
      cbTopsCell.DisplayMember = cbTopsCell.Items.Item(i).Name
      cbTopsCell.ValueMember = cbTopsCell.DisplayMember
End If

Where column index of 3 is my "Top" column. This just makes the cell go blank after I select an item from the list.
0
 
LVL 24

Expert Comment

by:alexey_gusev
ID: 33455504
no no, I meant that you have to define 2 public properties in your struct which you then set as Display and Value Member for the combobox
0
 

Author Comment

by:diablo089
ID: 33455735
Can you give an example?
0
 

Author Comment

by:diablo089
ID: 33456900
Fixed it.

This article describes the method you suggested of setting DisplayMember and ValueMember for the DataGridViewComboBoxColumn:

http://msdn.microsoft.com/en-us/library/ms404353.aspx

Thanks!
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Suggested Solutions

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

733 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