Results 1 to 17 of 17

Thread: Read Combobox selected Value From Datagridview instead of bound value

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    May 2004
    Location
    South Charleston, WV, USA
    Posts
    607

    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?

  2. #2
    Hyperactive Member
    Join Date
    Jun 2009
    Posts
    307

    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.

  3. #3
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,714

    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)

  4. #4
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,714

    Re: Read Combobox selected Value From Datagridview instead of bound value

    Quote Originally Posted by projecttoday View Post
    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.

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    May 2004
    Location
    South Charleston, WV, USA
    Posts
    607

    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.

  6. #6
    Hyperactive Member
    Join Date
    Jun 2009
    Posts
    307

    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?

  7. #7
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,714

    Re: Read Combobox selected Value From Datagridview instead of bound value

    Quote Originally Posted by projecttoday View Post
    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

  8. #8

    Thread Starter
    Fanatic Member
    Join Date
    May 2004
    Location
    South Charleston, WV, USA
    Posts
    607

    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.

  9. #9
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,714

    Re: Read Combobox selected Value From Datagridview instead of bound value

    Quote Originally Posted by projecttoday View Post
    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.

  10. #10

    Thread Starter
    Fanatic Member
    Join Date
    May 2004
    Location
    South Charleston, WV, USA
    Posts
    607

    Re: Read Combobox selected Value From Datagridview instead of bound value

    But I need to get the name, not the id number.

  11. #11
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,714

    Re: Read Combobox selected Value From Datagridview instead of bound value

    Quote Originally Posted by projecttoday View Post
    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.

  12. #12

    Thread Starter
    Fanatic Member
    Join Date
    May 2004
    Location
    South Charleston, WV, USA
    Posts
    607

    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

  13. #13
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,714

    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" _
        }

  14. #14

    Thread Starter
    Fanatic Member
    Join Date
    May 2004
    Location
    South Charleston, WV, USA
    Posts
    607

    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?

  15. #15
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,714

    Re: Read Combobox selected Value From Datagridview instead of bound value

    Quote Originally Posted by projecttoday View Post
    Using this code is it possible to concatenate other colums from the datagridview onto it?
    Please indicate what exactly you would want.

  16. #16
    New Member
    Join Date
    Apr 2012
    Posts
    9

    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

  17. #17
    New Member
    Join Date
    Apr 2012
    Posts
    9

    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
  •  



Click Here to Expand Forum to Full Width