Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

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

Posted on 2004-09-13
5
Medium Priority
?
226 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
  • 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 2000 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

[Webinar] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

Question has a verified solution.

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

Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
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…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Kernel Data Recovery is a renowned Data Recovery solution provider which offers wide range of softwares for both enterprise and home users with its cost-effective solutions. Let's have a quick overview of the journey and data recovery tools range he…
Suggested Courses
Course of the Month12 days, 5 hours left to enroll

564 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