Solved

semi advanced databinding question - combobox is filled with ID and Description - sample code pasted

Posted on 2004-09-13
5
219 Views
Last Modified: 2010-04-23
hi, my problem is that i have 2 comboboxes,
combobox1 has its databinding set to a dataview , and thus is not filled programmatically
combobox2 has been filled programmatically
thus
for each item in the dataview2
 add ItemID  & ItemDesc
next item

the adding of the item is done via a class called fillcombo (found on e-e)
now both comboboxes have got databindings enabled
and when the user selects a item in combobox1, the appropriate binded item must be selected in combobox2.
this is no problem and it all works fine, i just need to hook up combobox2 to the description field and not the ID field.

there are 2 tables,
table1 = ProductDescription
ProdID ProdDesc   ColorID
1         Porsche        1
2         Ferrari         2
3         Lamborghini 1

table2 = Color
ColorID   ColorDesc
1            Green
2            Red

this is my databinding add:

        Me.ComboBox1.DataSource = dv
        Me.ComboBox1.DisplayMember = "ProdDesc"
        Me.ComboBox2.DataBindings.Add("Text", dv, "ColorID")

i dont know how to set the last line, i also need to be able to update the data back to the database if for e.g. the user
is in "Edit Mode" and changes the item in combobox2 to another item, but i could possibly deduct all that once i am hopefully helped with this.

please paste the following code if willing to help: (dataset at bottom)
Public Class Form1
    Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
    Public Sub New()
        MyBase.New()
        InitializeComponent()
    End Sub
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub
    Private components As System.ComponentModel.IContainer
    Friend WithEvents ComboBox1 As System.Windows.Forms.ComboBox
    Friend WithEvents Dataset11 As IDDescriptionSelect.Dataset1
    Friend WithEvents ComboBox2 As System.Windows.Forms.ComboBox
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.ComboBox1 = New System.Windows.Forms.ComboBox
        Me.Dataset11 = New IDDescriptionSelect.Dataset1
        Me.ComboBox2 = New System.Windows.Forms.ComboBox
        CType(Me.Dataset11, System.ComponentModel.ISupportInitialize).BeginInit()
        Me.SuspendLayout()
        Me.ComboBox1.Location = New System.Drawing.Point(16, 32)
        Me.ComboBox1.Name = "ComboBox1"
        Me.ComboBox1.Size = New System.Drawing.Size(168, 21)
        Me.ComboBox1.TabIndex = 0
        Me.ComboBox1.Text = "ComboBox1"
        Me.Dataset11.DataSetName = "Dataset1"
        Me.Dataset11.Locale = New System.Globalization.CultureInfo("en-US")
        Me.ComboBox2.Location = New System.Drawing.Point(16, 72)
        Me.ComboBox2.Name = "ComboBox2"
        Me.ComboBox2.Size = New System.Drawing.Size(168, 21)
        Me.ComboBox2.TabIndex = 1
        Me.ComboBox2.Text = "ComboBox2"
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(400, 245)
        Me.Controls.Add(Me.ComboBox2)
        Me.Controls.Add(Me.ComboBox1)
        Me.Name = "Form1"
        Me.Text = "Form1"
        CType(Me.Dataset11, System.ComponentModel.ISupportInitialize).EndInit()
        Me.ResumeLayout(False)

    End Sub

#End Region

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ' HERE I FILL THE DATASET WITH GENERATED VALUES
        'add a new row to product datatable
        Dim ProductRow As Dataset1.ProductRow = Dataset11.Product.NewProductRow
        ProductRow.ProdID = "1"
        ProductRow.ProdDesc = "Porsche"
        ProductRow.ColorID = 1
        Dataset11.Product.AddProductRow(ProductRow)

        Dim ProductRow2 As Dataset1.ProductRow = Dataset11.Product.NewProductRow
        ProductRow2.ProdID = "2"
        ProductRow2.ProdDesc = "Ferrari"
        ProductRow2.ColorID = 2
        Dataset11.Product.AddProductRow(ProductRow2)

        Dim ProductRow3 As Dataset1.ProductRow = Dataset11.Product.NewProductRow
        ProductRow3.ProdID = "3"
        ProductRow3.ProdDesc = "Lamborghini"
        ProductRow3.ColorID = 1
        Dataset11.Product.AddProductRow(ProductRow3)

        'add  new row to color datatable
        Dim ColorRow As Dataset1.ColorRow = Dataset11.Color.NewColorRow
        ColorRow.ColorID = "1"
        ColorRow.ColorDesc = "Green"
        Dataset11.Color.AddColorRow(ColorRow)

        Dim ColorRow2 As Dataset1.ColorRow = Dataset11.Color.NewColorRow
        ColorRow2.ColorID = "2"
        ColorRow2.ColorDesc = "Red"
        Dataset11.Color.AddColorRow(ColorRow2)

        ComboBox1.Items.Clear()
        Dim dv As DataView = Dataset11.Tables("Product").DefaultView

        ComboBox2.Items.Clear()
        Dim dv2 As DataView = Dataset11.Tables("Color").DefaultView
        Dim i As Integer
        For i = 0 To Dataset11.Tables("Color").Rows.Count - 1
            With ComboBox2
                .Items.Add(New FillCombo(dv2(i)(1), dv2(i)(0)))
                .SelectedIndex = 0
            End With
        Next i

        Me.ComboBox1.DataSource = dv
        Me.ComboBox1.DisplayMember = "ProdDesc"
        ' I NEED TO FIX THIS DATABINDING SO THAT IT SHOWS THE COLORDESC AND NOT THE COLORID
        Me.ComboBox2.DataBindings.Add("Text", dv, "ColorID")
    End Sub

End Class
Public Class FillCombo
    Private sName As String
    Private iID As String
    Public Sub New()
        sName = ""
        iID = 0
    End Sub
    Public Sub New(ByVal Name As String, ByVal ID As String)
        sName = Name
        iID = ID
    End Sub
    Public Property Name() As String
        Get
            Return sName
        End Get
        Set(ByVal sValue As String)
            sName = sValue
        End Set
    End Property
    Public Property ItemData() As Int32
        Get
            Return iID
        End Get
        Set(ByVal iValue As Int32)
            iID = iValue
        End Set
    End Property
    Public Overrides Function ToString() As String
        Return sName
    End Function
End Class



'--------------------------------------
'dataset   called dataset1


<?xml version="1.0" encoding="utf-8" ?>
<xs:schema id="Dataset1" targetNamespace="http://tempuri.org/Dataset1.xsd" elementFormDefault="qualified"
      attributeFormDefault="qualified" xmlns="http://tempuri.org/Dataset1.xsd" xmlns:mstns="http://tempuri.org/Dataset1.xsd"
      xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
      <xs:element name="Dataset1" msdata:IsDataSet="true">
            <xs:complexType>
                  <xs:choice maxOccurs="unbounded">
                        <xs:element name="Product">
                              <xs:complexType>
                                    <xs:sequence>
                                          <xs:element name="ProdID" type="xs:int" minOccurs="0" />
                                          <xs:element name="ProdDesc" type="xs:string" minOccurs="0" />
                                          <xs:element name="ColorID" type="xs:int" minOccurs="0" />
                                    </xs:sequence>
                              </xs:complexType>
                        </xs:element>
                        <xs:element name="Color">
                              <xs:complexType>
                                    <xs:sequence>
                                          <xs:element name="ColorID" type="xs:int" minOccurs="0" />
                                          <xs:element name="ColorDesc" type="xs:string" minOccurs="0" />
                                    </xs:sequence>
                              </xs:complexType>
                        </xs:element>
                  </xs:choice>
            </xs:complexType>
      </xs:element>
</xs:schema>
0
Comment
Question by:jxharding
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 25

Expert Comment

by:RonaldBiemans
ID: 12045020
Hi jxharding, I think you are making it to hard for yourself

can't you do something like this

 Me.ComboBox1.ValueMember = "ColorID"
 Me.ComboBox1.DisplayMember = "ProdDesc"
 Me.ComboBox1.DataSource = dv

 Me.ComboBox1.ValueMember = "ColorID"
 Me.ComboBox1.DisplayMember = "ColorDesc"
me.combobox2.datasource = dv2

ComboBox2.DataBindings.Add("text", dv, "ColorID")

0
 

Author Comment

by:jxharding
ID: 12051816
hi ronaldbiemans,
i now understand that filling the second combobox programmatically is not so bright.
 so i tried this (thanks to the help of your last post):
        Me.ComboBox1.ValueMember = "ProdID"
        Me.ComboBox1.DisplayMember = "ProdDesc"
        Me.ComboBox1.DataSource = dv

        Me.ComboBox2.ValueMember = "ColorID"
        Me.ComboBox2.DisplayMember = "ColorDesc"
        Me.ComboBox2.DataSource = dv2

        ComboBox2.DataBindings.Add("Text", dv, "ColorID")

and now all is better ,except just that the last combobox shows the ID and not the description,
which is weird because the displaymember is set to "ColorDesc"
i also noticed that even if i set the valuemember of combobox1 to "prodid" or "colorid", it does not matter: e.g.
  Me.ComboBox1.ValueMember = "ProdID"
  Me.ComboBox1.ValueMember = "ColorID"
the binding is still fine, so does the combobox1's valuemember not make a difference then?
thank you very much!

0
 
LVL 25

Accepted Solution

by:
RonaldBiemans earned 500 total points
ID: 12052249
Sorry JxHarding, I made a small mistake in the above code it should be

ComboBox2.DataBindings.Add("SelectedValue", dv, "ColorID")

now you should get the ColorDesc instead of the ID



0
 

Author Comment

by:jxharding
ID: 12052315
i'll definitely be able to use this knowledge in prob most of my future projects
thank you so much!
0
 
LVL 25

Expert Comment

by:RonaldBiemans
ID: 12052336
Always glad to help, Jxharding :-)

databinding in .net takes a bit of getting used to.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Change Locale for Crystal Reports Dynamically in VB.Net 2010 Winforms. 3 32
Access Schema 6 45
vb.net background worker crossthread 11 30
vb.net datagrid point 4 21
Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question