Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Shaun E on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

What is ComboBox's equiv. of ".Databind()"?

Status
Not open for further replies.

RebLazer

Programmer
Jun 7, 2002
438
US
I am trying to (hand) code a dropdown list that gets populated from the DB with the following code:[tt]

Dim strConn As String = "server=richard;database=reports;uid=brownFox;password=lazyDog"
Dim conn As New SqlConnection(strConn)
Dim myCommand As New SqlDataAdapter("grant_selection_source_dd", strConn)
Dim mydad As New SqlDataAdapter()
mydad.SelectCommand = New SqlCommand("grant_selection_source_dd", conn)
mydad.SelectCommand.CommandType = CommandType.StoredProcedure
Dim ds As New DataSet()
mydad.Fill(ds)
grant_source_dd.DataSource = ds
grant_source_dd.databind()

[/tt]The problem is is that "databind()" is not a valid property for Windows Applications. I used this code in a Web App, but how do I do it for a Win App? When I run the above code (with the last line commented-out), the combobox reads "[tt]System.Data.DataViewManagerListItem[/tt]"

What is the right way to do this?

Thanks very much!
Lazer
 
Hi,
a couple of things. in a windows app there is no need to do the databind thing. also i could not make your code work so I tweaked it a bit. you had no tablemapping for the dataadapter. here is code that works to a product list in the northwind database.


Code:
        Dim mycommand As New System.Data.SqlClient.SqlCommand
        With mycommand
            .Connection = myconn
            .CommandText = "getProduct"
            .CommandType = CommandType.StoredProcedure
        End With
        Dim mydad As New system.Data.SqlClient.SqlDataAdapter
        mydad.SelectCommand = mycommand
            mydad.TableMappings.AddRange(New System.Data.Common.DataTableMapping() {New System.Data.Common.DataTableMapping("Table", "getProduct", New System.Data.Common.DataColumnMapping() {New System.Data.Common.DataColumnMapping("ProductID", "ProductID"), New System.Data.Common.DataColumnMapping("productname", "productname")})})

        Dim ds As New System.Data.DataSet
        mydad.Fill(ds)
            ListBox1.DataSource = ds
            ListBox1.DisplayMember = "getProduct.productname"
            ListBox1.ValueMember = "getProduct.ProductID"
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
[\code]


hope that helps

bassguy
 
GRR I missed 2 lines

Here is the correct code:


Code:
 Dim myconn As New System.Data.SqlClient.SqlConnection
        myconn.ConnectionString = "workstation id=OTC1269423;packet size=4096;integrated security=SSPI;data source=CoUDEV;persist security info=False;initial catalog=Northwind"
        Try
        Dim mycommand As New System.Data.SqlClient.SqlCommand
        With mycommand
            .Connection = myconn
            .CommandText = "getProduct"
            .CommandType = CommandType.StoredProcedure
        End With
        Dim mydad As New System.Data.SqlClient.SqlDataAdapter
        mydad.SelectCommand = mycommand
            mydad.TableMappings.AddRange(New System.Data.Common.DataTableMapping() {New System.Data.Common.DataTableMapping("Table", "getProduct", New System.Data.Common.DataColumnMapping() {New System.Data.Common.DataColumnMapping("ProductID", "ProductID"), New System.Data.Common.DataColumnMapping("productname", "productname")})})

        Dim ds As New System.Data.DataSet
        mydad.Fill(ds)
            ListBox1.DataSource = ds
            ListBox1.DisplayMember = "getProduct.productname"
            ListBox1.ValueMember = "getProduct.ProductID"
        Catch ex As Exception
            MsgBox(ex.ToString)


again hope this helps
 
Something else you may be interested in is not using databinding at all. My prefered method is to load the information into a dataset, and then set the boxes value according to the dataset. I find that this allows for a lot more functionality when it comes to controling the state of the form as well as firing off events. So basically, I acquire the information that i need to populate the valuemember, displaymember, and datasource (I do this through an adapter). Then I have some routines to select the appropriate value of the box according the current record by something like:
combobox.selectedvalue = Dataset.Tables("Table Name").Rows(rownumber).Item("ColumnName")
Then when it comes to updating you can use the same command, just in reverse:
Dataset.Tables("Table Name").Rows(rownumber).Item("ColumnName") = combobox.selectedvalue
when it comes to adding, you just create new rows and add them to the table in the dataset, then update the dataset.
example of adding:
Dim newRow As DataRow = dataset.Tables("Table Name").NewRow()
newRow("ColumnName") = combobox.selectedvalue

And then you update the dataset using whichever method you prefer.

Or, if you simply want to use the combobox and databinding, I only use 4 lines to do so:

combobox.DataSource = Dataset.Tables("TableName")
combobox.DisplayMember = "ColumnName"
combobox.ValueMember = "ColumnName"
combobox.DataBindings.Add _
(New Binding("SelectedValue", Dataset, "Table.Column"))
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top