|
-
Oct 1st, 2011, 06:48 PM
#1
Thread Starter
Fanatic Member
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?
-
Oct 1st, 2011, 07:04 PM
#2
Hyperactive Member
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)
Last edited by dkahn; Oct 1st, 2011 at 07:29 PM.
-
Oct 1st, 2011, 08:34 PM
#3
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)
-
Oct 1st, 2011, 08:37 PM
#4
Re: Read Combobox selected Value From Datagridview instead of bound value
 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.
-
Oct 1st, 2011, 09:31 PM
#5
Thread Starter
Fanatic Member
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.
-
Oct 1st, 2011, 11:24 PM
#6
Hyperactive Member
Re: Read Combobox selected Value From Datagridview instead of bound value
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?
-
Oct 1st, 2011, 11:34 PM
#7
Re: Read Combobox selected Value From Datagridview instead of bound value
 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
-
Oct 2nd, 2011, 01:02 AM
#8
Thread Starter
Fanatic Member
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.
-
Oct 2nd, 2011, 01:35 AM
#9
Re: Read Combobox selected Value From Datagridview instead of bound value
 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.
-
Oct 2nd, 2011, 08:30 AM
#10
Thread Starter
Fanatic Member
Re: Read Combobox selected Value From Datagridview instead of bound value
But I need to get the name, not the id number.
-
Oct 2nd, 2011, 09:27 AM
#11
Re: Read Combobox selected Value From Datagridview instead of bound value
 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.
-
Oct 2nd, 2011, 10:06 AM
#12
Thread Starter
Fanatic Member
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
-
Oct 2nd, 2011, 10:28 AM
#13
Re: Read Combobox selected Value From Datagridview instead of bound value
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" _
}
-
Oct 2nd, 2011, 11:06 AM
#14
Thread Starter
Fanatic Member
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?
-
Oct 2nd, 2011, 07:05 PM
#15
Re: Read Combobox selected Value From Datagridview instead of bound value
 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.
-
Aug 29th, 2012, 08:12 PM
#16
New Member
Re: Read Combobox selected Value From Datagridview instead of bound value
use this to get the displayed text rather then the bound value
ConversationTblDataGridView.Rows(icounter).Cells("PersonID").FormattedValue
-
Aug 29th, 2012, 08:18 PM
#17
New Member
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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|