Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Binding a Combo with Fixed Values - It can't be that complex surely!!

Posted on 2004-12-01
4
Medium Priority
?
185 Views
Last Modified: 2010-04-23
I have a combo which I need to populate with fixed values (e.g. OK,Yes,No etc..) and bind to a column in a table, which just happens to be displayed in a datagrid on the same form. Because the values are fixed, I figured it should be the same as binding a textbox object, it's just that the user doesn't have to type in the values, they can select them from the list (or so I thought). I added a textbox and bound that and it works fine. I added the combo, populated it but it doesn't change the values. It will show changes if I enter them via the text box.

For example:

1) Go to grid row 1, Data column (one I need to change) is '*' by default. Change this with combo to 'Yes', move off row 1 nothing changes.
2) Go back to grid row 1, change data column using textbox to 'Yes', cell in grid changes to 'Yes', combo changes to 'Yes', move off row and return and both textbox and combo show 'Yes'.

So the combo will show the data correctly, but cannot change the data.

I had posted this problem before but had to move onto another project before a solution could be found.
0
Comment
Question by:VFPSQLDeveloper
  • 2
  • 2
4 Comments
 

Author Comment

by:VFPSQLDeveloper
ID: 12717220
Sorry, meant to show the code used to do this:

        ' Join Valve Parameters & Group Tables and Populate Grid
        Dim cn As New SqlCeConnection
        cn.ConnectionString = _
               "Data Source=\My Documents\PmsCE.sdf;" + _
              "SSCE:Database Password=Pipelines;"
        cn.Open()

        Dim cmd As String
        Dim dr As SqlCeDataReader
        Dim count As Int16

        Try

            ' Check Location Exists for Pipelines
            Dim cm As New SqlCeCommand("SELECT Id FROM ValveLocation " & _
                "WHERE Pipeline_Id = " & Me.ComboBox1.SelectedValue & _
                " AND StrKey = '" & CStr(Me.TextBox1.Text) & "'", cn)

            ' Make Sure we have a matching Location (There must be a better way!)
            count = 0
            dr = cm.ExecuteReader()
            While dr.Read()
                count += 1
            End While

            ' Tell User if Duff Location
            If count = 0 Then
                MessageBox.Show("Location Not Valid for this Pipeline!")
                Return
            End If

            ' Fill Dataset with Parameters for Location
            cmd = "SELECT ValveGroup.Description, ValveParameters.Description, " & _
                "Prompt_Type " & _
                "FROM ValveParameters " & _
                "INNER JOIN ValveGroup ON Group_Id = ValveGroup.Id " & _
                "WHERE Pipeline_Id = " & Me.ComboBox1.SelectedValue & _
                " ORDER BY ValveGroup.Description, ValveParameters.Description"
            Dim da As New SqlCeDataAdapter(cmd, cn)
            Dim ds As New DataSet
            da.Fill(ds, "Parameters")

            ' Load Location Selection Form    
            Dim dlgLocation_Selection As New Location_Selection

            ' Populate the Grid
            Dim c As New DataColumn("Data", GetType(System.String))
            c.DefaultValue = "*"
            ds.Tables(0).Columns.Add(c)

            dlgLocation_Selection.DataGrid1.DataSource = ds.Tables(0)


            Dim dt As DataTable
            dt = ds.Tables("Parameters")

            ' Use Table Styles to Size Columns, Name Headers & and Make Id Columns Hidden
            Dim grdts As New DataGridTableStyle
            grdts.MappingName = dt.TableName
            dlgLocation_Selection.DataGrid1.TableStyles.Add(grdts)
            dlgLocation_Selection.DataGrid1.TableStyles(0).GridColumnStyles(0).Width = 70
            dlgLocation_Selection.DataGrid1.TableStyles(0).GridColumnStyles(0).HeaderText = "Group"
            dlgLocation_Selection.DataGrid1.TableStyles(0).GridColumnStyles(1).Width = 75
            dlgLocation_Selection.DataGrid1.TableStyles(0).GridColumnStyles(1).HeaderText = "Description"
            dlgLocation_Selection.DataGrid1.TableStyles(0).GridColumnStyles(2).Width = 25
            dlgLocation_Selection.DataGrid1.TableStyles(0).GridColumnStyles(2).HeaderText = "Type"

            'Populate Combo
            dlgLocation_Selection.ComboBox1.Items.Add("OK")
            dlgLocation_Selection.ComboBox1.Items.Add("Yes")
            dlgLocation_Selection.ComboBox1.Items.Add("No")
            dlgLocation_Selection.ComboBox1.Items.Add("*")

            ' Bind the textBox for Testing only
            dlgLocation_Selection.TextBox1.DataBindings.Add("Text", dt, "Data")

            ' Bind the Combo
            dlgLocation_Selection.ComboBox1.DataBindings.Add("Text", dt, "Data")

            ' Now Show the Dialog
            Me.Visible = False

            dlgLocation_Selection.ShowDialog()

            Me.Visible = True

            ' Release Data Adapter & Connection
            da.Dispose()
            cn.Dispose()
            cm.Dispose()

        Catch sqlex As SqlCeException

            ShowErrors(sqlex)

        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
0
 
LVL 1

Expert Comment

by:cmellon
ID: 12719233
Not read the full question yet, but noticed the comment about getting a count of the records to check whether there is one or not.  Best way to do this is like below.

        oCMD = cn.CreateCommand
        oCMD.CommandText = "SELECT Count(Id) FROM ValveLocation " & _
                "WHERE Pipeline_Id = " & Me.ComboBox1.SelectedValue & _
                " AND StrKey = '" & CStr(Me.TextBox1.Text) & "'"

        count = CType(oCMD.ExecuteScalar(), Integer)

       That will give you the count without having to use a datareader, problem with the way your doing it above is that while you do a dr.Read() it keeps a connection to the database, and when you call it again, i.e. loop through you are going back and forth between the database everytime.

The count code above will goto the database, count how many records match the criteria and return you the count, it is much more efficient than the way you have above.

Hope this helps.

I will read you post properly now and see if I can help you out with the main question

Craig
0
 
LVL 1

Accepted Solution

by:
cmellon earned 2000 total points
ID: 12719518
Just tried to do a quick test of what you are doing.

I think i have it how you want now,

Instead of binding to the Text value in the combobox, bind it to the SelectedItem value.

dlgLocation_Selection.ComboBox1.DataBindings.Add("SelectedItem", dt, "Data")

Let me know if this works

Regards

Craig
0
 

Author Comment

by:VFPSQLDeveloper
ID: 12720898
Fixed! Many thanks Craig I'm embarassed to say how long I wasted on that!! Also thanks for the advice on obtaining the count.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses
Course of the Month21 days, 2 hours left to enroll

810 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