Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Problem with DataGridViewComboBoxCell and Structured Data

Posted on 2010-08-16
5
Medium Priority
?
346 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 2000 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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 …
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

705 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