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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Possible to use Combo box to get data from bound record? 2

Status
Not open for further replies.

CraigBest

Programmer
Aug 1, 2001
545
US
OK another silly question, probably. I set up a query against my database to fill a data table, and bound the combo box to it to have it display the values of one of the fields. Now, in that data table I also have other fields on the same record, including two I'd like to display in text boxes next to the combo box.

So if I make a selection on the bound combo box does it set the current record on the data table, and can I then read the other values on the same row to display in the text boxes?

If not, what's the best way to set the data table's current record to the same one the Combo box is using so I can get the associated fields?

Thanks



CraigHartz
 



Yes, each textbox can be bound to a field in your table/query to reflect the data in the current row.

Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
OK, so if I just bind my text boxes to the same data table they will change with the combo box when its value is changed? Cool, thanks.

CraigHartz
 
Hmmm, I think I need more help. There's no DataSource or DisplayMember properties to bind to the data table on the textbox control. I figure if I could identify the current record in the table I could read out the values from the data table, but I'm not sure how to do that...

Essentially, I have this:

txtSpotsAvailable.Text = dsHereToThere.Tables("dtSiding").Rows(?).item("FieldName").tostring

How do I figure out what to put for an index value where the "?" is? Or id there another way to do this?

Thanks

CraigHartz
 
Is this what you are looking to do? Try this out on a test form.
Code:
Public Class Form5
    Dim DT As New DataTable
    Private Sub Form5_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        DT.TableName = "Table1"
        DT.Columns.Add("Column1", System.Type.GetType("System.String"))
        DT.Columns.Add("Column2", System.Type.GetType("System.Int32"))
        DT.Columns.Add("Column3", System.Type.GetType("System.Decimal"))

        Dim rnd As New Random()

        DT.Rows.Add(New Object() {"Row 1", 1, Convert.ToDecimal(rnd.NextDouble)})
        DT.Rows.Add(New Object() {"Row 2", 2, Convert.ToDecimal(rnd.NextDouble)})
        DT.Rows.Add(New Object() {"Row 3", 3, Convert.ToDecimal(rnd.NextDouble)})

        Dim cbo As New ComboBox
        cbo.Left = 12
        cbo.Top = 12
        Me.Controls.Add(cbo)

        Dim txt2 As New TextBox
        Me.Controls.Add(txt2)
        txt2.Left = 150
        txt2.Top = 12

        Dim txt3 As New TextBox
        Me.Controls.Add(txt3)
        txt3.Left = 300
        txt3.Top = 12

        'Databinding
        cbo.DataSource = DT
        cbo.DisplayMember = "Column1"
        cbo.ValueMember = "Column1"

        txt2.DataBindings.Add("Text", DT, "Column2")
        txt3.DataBindings.Add("Text", DT, "Column3")
    End Sub
End Class
 
Yes, thanks - the DataBindings property of the textbox were what I was looking for. Thanks a bunch!

CraigHartz
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top