Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Problem with DataGridViewComboBoxCell and Structured Data

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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

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 …
Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Screencast - Getting to Know the Pipeline

972 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