Solved

Problem with DataGridViewComboBoxCell and Structured Data

Posted on 2010-08-16
5
344 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

Enroll in July's Course of the Month

July's Course of the Month is now available! Enroll to learn HTML5 and prepare for certification. It's free for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

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

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…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Suggested Courses

623 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