none
Obtaining value from dataset without DataBinding RRS feed

  • Question

  • I have a form where various Textbox and ComboBox controls are DataBound (1 ComboBox selection selects the records )

    However I want to add 2 entries together before displaying in a new textbox.

    Whilst this can be done by accessing the text on the TextBox (i.e. Textbox1.Text =TextBox 2.Text & TextBox3.Text), how can I access the same data without having to databind to Textbox 2 and 3 in example ?

     

     

    Sunday, February 5, 2012 3:13 PM

Answers

  • Thank you - Got it (just added DataSet and Item and it works)

    Textbox1.Text = MyDataSet.MyDataTable.Rows(bs.Position).Item("code").ToString() & MyDataSet.MyDataTable.Rows(bs.Position).Item("description").ToString()

    I thing I Needed to do it this way as I wanted to work on the data first before passing to new textbox i.e. ignore Blank entries and adding items to string. 

     

    • Marked as answer by GFSIan Sunday, February 5, 2012 4:41 PM
    Sunday, February 5, 2012 4:41 PM

All replies

  • You'd need to at least know the row in the DataTable that has been "selected" by the ComboBox. I'll make an assumption about your databinding, so that I can give you a good example. I'll assume that you are using a BindingSource (I'll call it bs). If this is not the way you're databinding, you'll have to post some code.

    In order to get data from the DataTable, you'd do the following:

    ' the syntax is different for C# and VB. I use C#, so I hope
    ' that I got the syntax correct for VB
    Textbox1.Text = MyDataTable.Rows(bs.Position).Items("code").ToString() & MyDataTable.Rows(bs.Position).Items("description").ToString()
    

    Is this what you wanted to know?

    A better solution though, would be to add a calculated column to the DataTable and databind to it:

    ' this will be automatically calculated
    MyDataTable.Columns.Add("CodeDesc")
    MyDataTable.Columns("CodeDesc").Expression = "code + description"
    
    ' then databind to this new column
    Textbox1.DataBindings.Add("Text", bs, "CodeDesc")
    
    

    ~~Bonnie Berent [C# MVP]

    geek-goddess-bonnie.blogspot.com
    Sunday, February 5, 2012 4:07 PM
  • Thank you - Got it (just added DataSet and Item and it works)

    Textbox1.Text = MyDataSet.MyDataTable.Rows(bs.Position).Item("code").ToString() & MyDataSet.MyDataTable.Rows(bs.Position).Item("description").ToString()

    I thing I Needed to do it this way as I wanted to work on the data first before passing to new textbox i.e. ignore Blank entries and adding items to string. 

     

    • Marked as answer by GFSIan Sunday, February 5, 2012 4:41 PM
    Sunday, February 5, 2012 4:41 PM
  • I guess you figured it out yourself then, before my reply. Cool.


    ~~Bonnie Berent [C# MVP]

    geek-goddess-bonnie.blogspot.com
    Sunday, February 5, 2012 5:01 PM