?
Solved

How to retrieve the Value of the displaymember of a DataGridViewComboboxColumn

Posted on 2016-10-04
2
Medium Priority
?
80 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 2000 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

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

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses

770 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