Results 1 to 7 of 7

Thread: How to populate textboxes with the data from a datagrid when a row is selected?

  1. #1

    Thread Starter
    New Member
    Join Date
    Jul 2011
    Posts
    12

    How to populate textboxes with the data from a datagrid when a row is selected?

    I currently have a datagrid that I have populated from a dataset and I need to be able to populate some textboxes on my form with the data is the datagrid on the row I select?

    I have only been learning VB.net in my new role for 2 weeks now and would appreciate any help.

    Thanks

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,422

    Re: How to populate textboxes with the data from a datagrid when a row is selected?

    you can bind the textboxes to seperate fields in the same datatable you've bound your dgv with. as you select a new row in the dgv, the textboxes will update with the information from that row.

    here's how you'd do it:

    Textbox1.DataBindings.Add("Text", [datatable], "fieldName")

  3. #3

    Thread Starter
    New Member
    Join Date
    Jul 2011
    Posts
    12

    Re: How to populate textboxes with the data from a datagrid when a row is selected?

    Morning, thanks for your reply.

    I've tried what you have put first thing when I got to work this morning and it is coming up with an error. It is sayin:

    'ArguamentException was unhandled' Cannot bind to property or column part_description on the datasource. Parameter name: datamember.

    After a bit of exploring the help files I have managed to display the text via the following method:

    txtPartDescription.Text = dgvPartsData.CurrentRow.Cells(1).FormattedValue.ToString

    I'm not sure if it is the best practise way of doing it though?

  4. #4
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,422

    Re: How to populate textboxes with the data from a datagrid when a row is selected?

    databinding is the best method. can you post the exact code you tried?

  5. #5

    Thread Starter
    New Member
    Join Date
    Jul 2011
    Posts
    12

    Re: How to populate textboxes with the data from a datagrid when a row is selected?

    I just added the following:

    Mainform.txtPartNumber.DataBindings.Add("Text", [partsDataSet], "part_description")

  6. #6
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,422

    Re: How to populate textboxes with the data from a datagrid when a row is selected?

    partsDataSet appears to be a dataset. you need to bind to a datatable

  7. #7

    Thread Starter
    New Member
    Join Date
    Jul 2011
    Posts
    12

    Re: How to populate textboxes with the data from a datagrid when a row is selected?

    Yes your correct it is a dataset. I will read up on how to bind it and give it a go.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width