Read Combobox selected Value From Datagridview instead of bound value
I'm loopiing through a datagridview and I want to get the selected value in a combobox, not the bound column's value. (I'm putting the contents of the dgv on a Word document.)
Code:
For icounter = 0 To Me.ConversationTblDataGridView.RowCount - 1
range.Text = range.Text & Me.ConversationTblDataGridView.Rows(icounter).Cells("PersonID").Value <-- I want to change "Value" so I get the displayed value, not the person's id
range.Text = range.Text & CHR(13)
Next icounter
It will not take .DisplayMember or .SelectedItem and .Selected returns True of False. Can anyone tell me how to get the name that shows in the combo box instead of the id number?
Re: Read Combobox selected Value From Datagridview instead of bound value
Is this what you're looking for?
Code:
'Get the index & Text value of the current combo selection in a datagridview
Dim myComboBoxCell As DataGridViewComboBoxCell = DirectCast(dgvAdd.Item(column, row), DataGridViewComboBoxCell)
Dim mText As String = MyComboBoxCell.Value
Dim SelectedIndex As Integer = myComboBoxCell.Items.IndexOf(myComboBoxCell.Value)
Re: Read Combobox selected Value From Datagridview instead of bound value
The following requires VS2008 or higher with Framework 3.5 or higher.
Tested on a bound ComboBox column.
Code:
Dim ColumnName As String = "PersonID"
Dim Result = String.Join(Environment.NewLine, _
( _
From D In DataGridView1.Rows.Cast(Of DataGridViewRow)() _
Where Not D.IsNewRow _
Select Item = D.Cells(ColumnName).Value.ToString).ToArray _
)
Console.WriteLine(Result)
Re: Read Combobox selected Value From Datagridview instead of bound value
Quote:
Originally Posted by
projecttoday
I'm loopiing through a datagridview and I want to get the selected value in a combobox, not the bound column's value. (I'm putting the contents of the dgv on a Word document.)
Code:
For icounter = 0 To Me.ConversationTblDataGridView.RowCount - 1
range.Text = range.Text & Me.ConversationTblDataGridView.Rows(icounter).Cells("PersonID").Value <-- I want to change "Value" so I get the displayed value, not the person's id
range.Text = range.Text & CHR(13)
Next icounter
It will not take .DisplayMember or .SelectedItem and .Selected returns True of False. Can anyone tell me how to get the name that shows in the combo box instead of the id number?
Make sure PersonID is the ColumnName in the DataGridView and not the underlying Data Column in the DataSource of the DataGridView.
Re: Read Combobox selected Value From Datagridview instead of bound value
dkahn,
When I run your code, mText contains the id, not the name.
kevininstructor,
PersonID is the name of the column and the name of the field in the data table. This is a VB.Net program.
Re: Read Combobox selected Value From Datagridview instead of bound value
Quote:
When I run your code, mText contains the id, not the name.
That code will give you the selected value of the combobox - the current text displayed in the combobox. That's not what you want?
Re: Read Combobox selected Value From Datagridview instead of bound value
Quote:
Originally Posted by
projecttoday
dkahn,
When I run your code, mText contains the id, not the name.
kevininstructor,
PersonID is the name of the column and the name of the field in the data table. This is a VB.Net program.
Did you try the code I suggested in post 3
Re: Read Combobox selected Value From Datagridview instead of bound value
dkahn, It's not getting the selected text, it's getting the id which is the value of the combobox.
kevininstructor, Your code is showing the entire column of id numbers each time it goes through the loop.
Re: Read Combobox selected Value From Datagridview instead of bound value
Quote:
Originally Posted by
projecttoday
dkahn, It's not getting the selected text, it's getting the id which is the value of the combobox.
kevininstructor, Your code is showing the entire column of id numbers each time it goes through the loop.
My code is meant to run by itself, no loop required.
Re: Read Combobox selected Value From Datagridview instead of bound value
But I need to get the name, not the id number.
Re: Read Combobox selected Value From Datagridview instead of bound value
Quote:
Originally Posted by
projecttoday
But I need to get the name, not the id number.
Well you should have mentioned this in your initial question which you did not. So more details from you are needed as I can not guess your requirements.
Re: Read Combobox selected Value From Datagridview instead of bound value
I did mention it. I have a datagridview that I want to pull everything off of and put on a Word document. Just as it is seen in the datagrid view. One of the columns is a combobox on a persons table. I want the name as it is seen on the datagridview, not the id field on which the combobox is based. I could put in some code that would look up the name from the persons table but I though there might be an easier way. This code delivers the id number. I have omitted the other colums for simplicity. Scroll it to the right.
Code:
For icounter = 0 To Me.ConversationTblDataGridView.RowCount - 1
range.Text = range.Text & Me.ConversationTblDataGridView.Rows(icounter).Cells("PersonID").Value <-- I want to change "Value" so I get the displayed value in this combobox, not the person's id
range.Text = range.Text & CHR(13)
Next icounter
Re: Read Combobox selected Value From Datagridview instead of bound value
Quote:
One of the columns is a combobox on a persons table. I want the name as it is seen on the datagridview, not the id field
This is what the code I supplied does, gets the text from the ComboBox column of the DataGridView, it does not get anything but that.
So change PersonID to the name of the ComboBox column (see example below)
Code:
Dim ColumnName As String = "PersonID"
Dim Result = String.Join(Environment.NewLine, _
( _
From D In DataGridView1.Rows.Cast(Of DataGridViewRow)() _
Where Not D.IsNewRow _
Select Item = D.Cells(ColumnName).Value.ToString).ToArray _
)
Console.WriteLine(Result)
So if I had a column defined as shown below I would be using ContactsColumn in the LINQ statement I gave you.
Code:
Dim ContactPosition As New DataGridViewComboBoxColumn With _
{ _
.DataPropertyName = "ContactPosition", _
.DataSource = dsPeople.Tables("PositionType"), _
.DisplayMember = "ContactPosition", _
.DisplayStyle = DataGridViewComboBoxDisplayStyle.Nothing, _
.Name = "ContactsColumn", _
.ValueMember = "ContactPosition" _
}
Re: Read Combobox selected Value From Datagridview instead of bound value
Using this code is it possible to concatenate other colums from the datagridview onto it?
Re: Read Combobox selected Value From Datagridview instead of bound value
Quote:
Originally Posted by
projecttoday
Using this code is it possible to concatenate other colums from the datagridview onto it?
Please indicate what exactly you would want.
Re: Read Combobox selected Value From Datagridview instead of bound value
use this to get the displayed text rather then the bound value
Quote:
ConversationTblDataGridView.Rows(icounter).Cells("PersonID").FormattedValue
Re: Read Combobox selected Value From Datagridview instead of bound value
use this to get the displayed text rather then the bound value
Code:
ConversationTblDataGridView.Rows(icounter).Cells("PersonID").FormattedValue