Solved

How to retrieve the Value of the displaymember of a DataGridViewComboboxColumn

Posted on 2016-10-04
2
67 Views
Last Modified: 2016-10-05
I have a reference to a cell of a DataGridViewComboboxColumn
"DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex)"
I can get the Value of the ValueMember using "DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Value"
But how can I retrieve the Value of the DisplayMember?
0
Comment
Question by:Declan_Basile
[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
2 Comments
 
LVL 34

Accepted Solution

by:
it_saige earned 500 total points
ID: 41828576
FormattedValue is the displayed value.  Proof of concept -

Form1.vb -
Imports System.Text

Public Class Form1
	Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
		DataGridView1.DataSource = (From i In Enumerable.Range(0, 5) Select New With {.ID = i, .Name = String.Format("Name{0}", i), .Money = CType((i + 9) * 5, Decimal), .BirthDate = DateTime.Now.AddYears(-(i * 9))}).ToList()
	End Sub

	Private Sub OnCellFormatting(sender As Object, e As DataGridViewCellFormattingEventArgs) Handles DataGridView1.CellFormatting
		If TypeOf sender Is DataGridView Then
			Dim grid = DirectCast(sender, DataGridView)
			If grid.Rows.Count > 0 And e.Value IsNot Nothing Then
				Dim row = DirectCast(grid.Rows(e.RowIndex), DataGridViewRow)
				Dim column = DirectCast(grid.Columns(e.ColumnIndex), DataGridViewColumn)
				If column.Name.IndexOf("money", StringComparison.OrdinalIgnoreCase) > -1 Then
					e.Value = CType(e.Value, Decimal).ToString("C2")
				ElseIf column.Name.IndexOf("birthdate", StringComparison.OrdinalIgnoreCase) > -1 Then
					e.Value = CType(e.Value, DateTime).ToString("MM/dd/yyyy")
				End If
			End If
		End If
	End Sub

	Private Sub OnClick(sender As Object, e As EventArgs) Handles Button1.Click
		If DataGridView1.SelectedRows.Count > 0 Then
			Dim builder As New StringBuilder()
			builder.AppendLine("Values:")
			builder.AppendLine(String.Join("; ", From cell As DataGridViewCell In DataGridView1.SelectedRows(0).Cells Select String.Format("{0}: {1}", DataGridView1.Columns(cell.ColumnIndex).Name, cell.Value)))
			builder.AppendLine()
			builder.AppendLine("Formatted Values:")
			builder.AppendLine(String.Join("; ", From cell As DataGridViewCell In DataGridView1.SelectedRows(0).Cells Select String.Format("{0}: {1}", DataGridView1.Columns(cell.ColumnIndex).Name, cell.FormattedValue)))
			MessageBox.Show(builder.ToString())
		End If
	End Sub
End Class

Open in new window

Form1.Designer.vb -
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Partial Class Form1
    Inherits System.Windows.Forms.Form

    'Form overrides dispose to clean up the component list.
    <System.Diagnostics.DebuggerNonUserCode()> _
    Protected Overrides Sub Dispose(ByVal disposing As Boolean)
        Try
            If disposing AndAlso components IsNot Nothing Then
                components.Dispose()
            End If
        Finally
            MyBase.Dispose(disposing)
        End Try
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    <System.Diagnostics.DebuggerStepThrough()> _
    Private Sub InitializeComponent()
		Me.DataGridView1 = New System.Windows.Forms.DataGridView()
		Me.Button1 = New System.Windows.Forms.Button()
		CType(Me.DataGridView1, System.ComponentModel.ISupportInitialize).BeginInit()
		Me.SuspendLayout()
		'
		'DataGridView1
		'
		Me.DataGridView1.AutoSizeColumnsMode = System.Windows.Forms.DataGridViewAutoSizeColumnsMode.Fill
		Me.DataGridView1.AutoSizeRowsMode = System.Windows.Forms.DataGridViewAutoSizeRowsMode.AllCells
		Me.DataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize
		Me.DataGridView1.Dock = System.Windows.Forms.DockStyle.Top
		Me.DataGridView1.Location = New System.Drawing.Point(0, 0)
		Me.DataGridView1.MultiSelect = False
		Me.DataGridView1.Name = "DataGridView1"
		Me.DataGridView1.RowHeadersVisible = False
		Me.DataGridView1.RowHeadersWidthSizeMode = System.Windows.Forms.DataGridViewRowHeadersWidthSizeMode.AutoSizeToAllHeaders
		Me.DataGridView1.SelectionMode = System.Windows.Forms.DataGridViewSelectionMode.FullRowSelect
		Me.DataGridView1.Size = New System.Drawing.Size(570, 224)
		Me.DataGridView1.TabIndex = 0
		'
		'Button1
		'
		Me.Button1.Location = New System.Drawing.Point(436, 230)
		Me.Button1.Name = "Button1"
		Me.Button1.Size = New System.Drawing.Size(122, 23)
		Me.Button1.TabIndex = 1
		Me.Button1.Text = "Get Row Values"
		Me.Button1.UseVisualStyleBackColor = True
		'
		'Form1
		'
		Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
		Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
		Me.ClientSize = New System.Drawing.Size(570, 261)
		Me.Controls.Add(Me.Button1)
		Me.Controls.Add(Me.DataGridView1)
		Me.Name = "Form1"
		Me.Text = "Form1"
		CType(Me.DataGridView1, System.ComponentModel.ISupportInitialize).EndInit()
		Me.ResumeLayout(False)

	End Sub
	Friend WithEvents DataGridView1 As System.Windows.Forms.DataGridView
	Friend WithEvents Button1 As System.Windows.Forms.Button

End Class

Open in new window

Produces the following output -Capture.JPG
-saige-
0
 
LVL 1

Author Closing Comment

by:Declan_Basile
ID: 41830147
Worked great.  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

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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…
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

734 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