populating a combo box with dataset - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

populating a combo box with dataset

rikb53rikb53 Posts: 64Member
guys, im using a loop and controlchars.crlf to populate a combo box,
but it makes one line only.
am i missing something in the properties box of the combo box to make this work?
here's the code.

nothing fancy at all;;

Dim o As Integer
Dim info2 As String
For o = 0 To (Ds1.tblMyTable.Count - 1)
info2 += (Ds1.tblMyTable(o).Name & ControlChars.CrLf)
Next o
cboPickem.Items.Add(info2)
Dim i As Integer

but it lists this all in a line. i know there's something simple that im missing here.
thanks
rik

Comments

  • natloznatloz Posts: 54Member
    This is how I populate a combo box from a dataset
    [code]
    'Pass connection string to SqlConnection
    oConn.ConnectionString = strConn

    '*****************************
    'Load the Branch Dataset Table
    '*****************************
    'SQL Database statements
    oComm = New SqlCommand
    oComm.CommandType = CommandType.StoredProcedure
    oComm.CommandText = "spGetBranch"
    oComm.Connection = oConn

    'Send in parameters (0 means all branches)
    oComm.Parameters.Add(New SqlParameter("@pkBranchID", SqlDbType.Int)).Value = 0

    'create and fill the Data Adapter
    Dim oDa As New SqlDataAdapter(oComm) 'SQL Data Adapter object
    oDa.Fill(_oDs, "tblBranch")

    With cboBranch
    .DataSource = _oDs.Tables("tblBranch")
    .ValueMember = "pkBranchID"
    .DisplayMember = "varBranchName"
    End With

    cboBranch.SelectedValue = intSelectedBranch

    'Remove parameters
    oComm.Parameters.Clear()
    [/code]

    Does this help?

    \--||--//

    //--||--\

  • natloznatloz Posts: 54Member
    [b][red]This message was edited by natloz at 2004-6-8 7:20:14[/red][/b][hr]
    : guys, im using a loop and controlchars.crlf to populate a combo box,
    : but it makes one line only.
    : am i missing something in the properties box of the combo box to make this work?
    : here's the code.
    :
    : nothing fancy at all;;
    :
    : Dim o As Integer
    : Dim info2 As String
    : For o = 0 To (Ds1.tblMyTable.Count - 1)
    : info2 += (Ds1.tblMyTable(o).Name & ControlChars.CrLf)
    : Next o
    : cboPickem.Items.Add(info2)
    : Dim i As Integer
    :
    : but it lists this all in a line. i know there's something simple that im missing here.
    : thanks
    : rik
    :

    From your code though...I think the

    [code]cboPickem.Items.Add(info2)[/code]
    should come before
    [code]Next o[/code]
    You probably should not use Info2 += but instead use Info2 =
    because I think you are concatenating a big string together
    the way you are doing it, and then writing that single string to the combo box.

    \--||--//

    //--||--\



  • rikb53rikb53 Posts: 64Member
    : This is how I populate a combo box from a dataset
    : [code]
    : 'Pass connection string to SqlConnection
    : oConn.ConnectionString = strConn
    :
    : '*****************************
    : 'Load the Branch Dataset Table
    : '*****************************
    : 'SQL Database statements
    : oComm = New SqlCommand
    : oComm.CommandType = CommandType.StoredProcedure
    : oComm.CommandText = "spGetBranch"
    : oComm.Connection = oConn
    :
    : 'Send in parameters (0 means all branches)
    : oComm.Parameters.Add(New SqlParameter("@pkBranchID", SqlDbType.Int)).Value = 0
    :
    : 'create and fill the Data Adapter
    : Dim oDa As New SqlDataAdapter(oComm) 'SQL Data Adapter object
    : oDa.Fill(_oDs, "tblBranch")
    :
    : With cboBranch
    : .DataSource = _oDs.Tables("tblBranch")
    : .ValueMember = "pkBranchID"
    : .DisplayMember = "varBranchName"
    : End With
    :
    : cboBranch.SelectedValue = intSelectedBranch
    :
    : 'Remove parameters
    : oComm.Parameters.Clear()
    : [/code]
    :
    : Does this help?
    :
    : \--||--//
    :
    : //--||--\
    :
    :

  • rikb53rikb53 Posts: 64Member
    i'll give it a try. my problem was not populating the combo box but, making it skip down the lines on each new value.
    i worked around it by making it a .txt file, then reading from that file thru an array into the combo box and it works. but it seems like a lot of effort for something really simple.
    i'll give your code a shot here sometime this afternoon and i really do appreciate the reply
    thanks again
    rik



    : : This is how I populate a combo box from a dataset
    : : [code]
    : : 'Pass connection string to SqlConnection
    : : oConn.ConnectionString = strConn
    : :
    : : '*****************************
    : : 'Load the Branch Dataset Table
    : : '*****************************
    : : 'SQL Database statements
    : : oComm = New SqlCommand
    : : oComm.CommandType = CommandType.StoredProcedure
    : : oComm.CommandText = "spGetBranch"
    : : oComm.Connection = oConn
    : :
    : : 'Send in parameters (0 means all branches)
    : : oComm.Parameters.Add(New SqlParameter("@pkBranchID", SqlDbType.Int)).Value = 0
    : :
    : : 'create and fill the Data Adapter
    : : Dim oDa As New SqlDataAdapter(oComm) 'SQL Data Adapter object
    : : oDa.Fill(_oDs, "tblBranch")
    : :
    : : With cboBranch
    : : .DataSource = _oDs.Tables("tblBranch")
    : : .ValueMember = "pkBranchID"
    : : .DisplayMember = "varBranchName"
    : : End With
    : :
    : : cboBranch.SelectedValue = intSelectedBranch
    : :
    : : 'Remove parameters
    : : oComm.Parameters.Clear()
    : : [/code]
    : :
    : : Does this help?
    : :
    : : \--||--//
    : :
    : : //--||--\
    : :
    : :
    :
    :

  • natloznatloz Posts: 54Member
    : i'll give it a try. my problem was not populating the combo box but, making it skip down the lines on each new value.
    : i worked around it by making it a .txt file, then reading from that file thru an array into the combo box and it works. but it seems like a lot of effort for something really simple.
    : i'll give your code a shot here sometime this afternoon and i really do appreciate the reply
    : thanks again
    : rik
    :

    Check out my other reply to you. It may be your problem.

    \--||--//

    //--||--\

  • rikb53rikb53 Posts: 64Member
    okay, i'll try that. but the strange thing is i can run that same code and show in a messsagebox and the controlchars.crlf behaves just fine.


    : [b][red]This message was edited by natloz at 2004-6-8 7:20:14[/red][/b][hr]
    : : guys, im using a loop and controlchars.crlf to populate a combo box,
    : : but it makes one line only.
    : : am i missing something in the properties box of the combo box to make this work?
    : : here's the code.
    : :
    : : nothing fancy at all;;
    : :
    : : Dim o As Integer
    : : Dim info2 As String
    : : For o = 0 To (Ds1.tblMyTable.Count - 1)
    : : info2 += (Ds1.tblMyTable(o).Name & ControlChars.CrLf)
    : : Next o
    : : cboPickem.Items.Add(info2)
    : : Dim i As Integer
    : :
    : : but it lists this all in a line. i know there's something simple that im missing here.
    : : thanks
    : : rik
    : :
    :
    : From your code though...I think the
    :
    : [code]cboPickem.Items.Add(info2)[/code]
    : should come before
    : [code]Next o[/code]
    : You probably should not use Info2 += but instead use Info2 =
    : because I think you are concatenating a big string together
    : the way you are doing it, and then writing that single string to the combo box.
    :
    : \--||--//
    :
    : //--||--\
    :
    :
    :
    :

  • natloznatloz Posts: 54Member
    : okay, i'll try that. but the strange thing is i can run that same code and show in a messsagebox and the controlchars.crlf behaves just fine.
    :
    The way you are currently doing it does not make sense to me.
    Each cbo.ADD represents a line in a combo box. You need one ADD for each and every line you want in the combo box.

    If you do it the same way as my first reply, the combo box automatically adds a line for each line in the Dataset, and also lets the combo box know which value to display as well as which value to use as a retrieval key.

    What you are doing would make sense if you were trying to format a block of text or a sentence. A Message Box should behave just fine because there is only one text property of a message box. (this would be the same for exporting to a text file or perhaps populating a Text box.)

    Let me know if this makes sense.

    \--||--//

    //--||--\

  • rikb53rikb53 Posts: 64Member
    that makes total sense. had to get away from work to be able to concentrate.
    by putting the combobox.add property in the loop, it adds then goes back until the count is satisfied.
    gee, that was so simple. and makes absolute perfect sense to me.
    thanks again.
    rik

    : : okay, i'll try that. but the strange thing is i can run that same code and show in a messsagebox and the controlchars.crlf behaves just fine.
    : :
    : The way you are currently doing it does not make sense to me.
    : Each cbo.ADD represents a line in a combo box. You need one ADD for each and every line you want in the combo box.
    :
    : If you do it the same way as my first reply, the combo box automatically adds a line for each line in the Dataset, and also lets the combo box know which value to display as well as which value to use as a retrieval key.
    :
    : What you are doing would make sense if you were trying to format a block of text or a sentence. A Message Box should behave just fine because there is only one text property of a message box. (this would be the same for exporting to a text file or perhaps populating a Text box.)
    :
    : Let me know if this makes sense.
    :
    : \--||--//
    :
    : //--||--\
    :
    :

Sign In or Register to comment.