none
How to replace a DataGridView with textboxes in VB2010 RRS feed

  • Question

  • I have created a a VB program using VB2010. It is bound to an access database with two tables, one is an Orders tables and one is an Items table. you can have several items in each order. this works ok at the moment with the Items displayed in a DataGridView, but i'm trying to replace the DataGridView with textboxes and other controls. however i can only view the first row with them.

    is there a way that i can use textboxes to show data instead of a datagrid view?

     

    i guess i would need something like:

    textbox1 = row(0)

    textbox1 = row(1)


    thanks very much.
    Monday, October 4, 2010 7:38 PM

Answers

  • Hi Rob,

    You'd have to use different BindingSources for different TextBoxes. For example, if you wanted to coordinate TextBox1 and TextBox2 to show data from the same row, but TextBox3 and TextBox4 should show data from a different row, you'd use one BindingSource for 1 and 2 and a different BindingSource for 3 and 4. Both BindingSources can be bound to the same DataTable.


    ~~Bonnie Berent [C# MVP]

    geek-goddess-bonnie.blogspot.com
    • Marked as answer by Glost Thursday, October 7, 2010 11:57 AM
    Tuesday, October 5, 2010 4:38 PM

All replies

  • You need to use DataBinding, just as you used DataBinding to bind your DataGridView. Please excuse any VB syntax errors, I'm a C#'er:

    ' First, set up your BindingSources
    BindingSource bsOrders = new BindingSource()
    Me.bsOrders.DataSource = Me.MyDataSet.Tables("Orders")
    BindingSource bsItems = new BindingSource()
    Me.bsItems.DataSource = Me.MyDataSet.Tables("Items")
    
    ' Then DataBind your TextBoxes
    Me.txtOrderNumber.DataBindings.Add("Text", Me.bsOrders, "OrderNum")
    Me.txtItemNumber.DataBindings.Add("Text", Me.bsItems, "ItemNum")
    ' etc.
    
    ' To move to the next row, use code like this (perhaps in a button click event)
    Me.bsItems.Position = Me.bsItems.Position + 1
    
    

    ~~Bonnie Berent [C# MVP]

    geek-goddess-bonnie.blogspot.com
    Tuesday, October 5, 2010 4:44 AM
  • Hi BonnieB

     

    Will this allow me to have the textboxes showing data from different row positions at the same time?

    Rob

    Tuesday, October 5, 2010 4:31 PM
  • Hi Rob,

    You'd have to use different BindingSources for different TextBoxes. For example, if you wanted to coordinate TextBox1 and TextBox2 to show data from the same row, but TextBox3 and TextBox4 should show data from a different row, you'd use one BindingSource for 1 and 2 and a different BindingSource for 3 and 4. Both BindingSources can be bound to the same DataTable.


    ~~Bonnie Berent [C# MVP]

    geek-goddess-bonnie.blogspot.com
    • Marked as answer by Glost Thursday, October 7, 2010 11:57 AM
    Tuesday, October 5, 2010 4:38 PM
  • Thanks very much Bonnie

    that worked a treat.

     

    Rob

    Thursday, October 7, 2010 1:50 PM
  • You're quite welcome, Rob ... glad I could help! =0)
    ~~Bonnie Berent [C# MVP]

    geek-goddess-bonnie.blogspot.com
    Thursday, October 7, 2010 3:28 PM
  • Spam on the MSDN forums ... how nice!!!
    ~~Bonnie Berent [C# MVP]

    geek-goddess-bonnie.blogspot.com
    Sunday, October 10, 2010 10:33 PM
  • Hi Bonnie

     

    I dont seem to be able to add a new record to my database,i can view it perfectly.

    If i click add record, fill in the data for the Order table and the Items table, and click save.

    The orders table saves correctly but the Items table does'nt. this work correctly with datagridview though.

    Is this because i need to tell the items table to add a record as well?

     

    regards

    Rob

    Monday, October 11, 2010 7:04 PM
  • Hi Rob,

    You say you click "add record" ... well, I don't know what that means. Do you have a button who's click event handler adds a record? You must have some code in there somewhere for adding records. If you need to add both an Order table row and an Items table row, then yes, you'll have to add both at some point to both of your DataSet DataTables.

    Could you post your code for adding the rows?


    ~~Bonnie Berent [C# MVP]

    geek-goddess-bonnie.blogspot.com
    Tuesday, October 12, 2010 3:36 PM