Solved

Problem with DataGridViewComboBoxCell and Structured Data

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

757 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

20 Experts available now in Live!

Get 1:1 Help Now