Results 1 to 3 of 3

Thread: [RESOLVED] Selecting value using combobox

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jul 2006
    Posts
    135

    [RESOLVED] Selecting value using combobox

    Hi All,

    is it possible for me to select an additional value from my datatable when selecting a value in a combobox?

    I have a sql query:

    SELECT KeyCol, Col2, Col3 FROM TestTable

    I bind the combo like so:

    cboTest.DataSource = dtTest
    cboTest.DisplayMember = "Col2"
    cboTest.SelectedItem = "KeyCol"
    cboTest.ValueMember = "KeyCol"

    Now what I would like to do is get the value of "Col3" when the user fires the selectedindex_changed event.

    Hope this is clear, any help appreciated.
    Last edited by CodeKiller; Apr 4th, 2007 at 07:45 AM.
    ---------------------------------------------------
    VB 2003
    SQL Server 2000

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Selecting value using combobox

    Firstly, that third line is not going to do anything useful.

    As for your question, when the user makes a selection the SelectedItem is a DataRowView. You can get any field you want from that, e.g.
    vb Code:
    1. Dim col3Val As String = CStr(DirectCast(myComboBox.SelectedItem, DataRowView)("Col3"))
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Jul 2006
    Posts
    135

    Re: Selecting value using combobox

    Excellent, thanks very much.
    Have also removed offending line.
    ---------------------------------------------------
    VB 2003
    SQL Server 2000

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