Results 1 to 12 of 12

Thread: Finding row position of a Data Table

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Apr 2006
    Posts
    17

    Finding row position of a Data Table

    Ive searched everywhere but cannot seem to find out how to find the row position/index in a data table, using a value from another table.

    For example, I use the row position from Linkingtable1 to get my value for CustomerID, but i want to use the CustomerID to search the CustomerTable for its row position, in order to show the Customers Name in a text box, As currently when loading the textbox shows the customerID. I understand the code to get the names from the rows in the datatable, its just finding the row position of the customerID.

    Bit of a dodgy explanation, but hope you kinda understand

    Thanks!

  2. #2
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: Finding row position of a Data Table

    You can use DataTable.DefaultView.Find or DataTable.DefaultView.RowFilter to find the desired row.

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Apr 2006
    Posts
    17

    Re: Finding row position of a Data Table

    I get two different errors when trying to use each of those ways.
    For Find i get:
    Code:
    Find finds a row based on a Sort order, and no Sort order is specified.
    For RowFilter i get:
    Code:
    Index was outside the bounds of the array.

  4. #4
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: Finding row position of a Data Table

    I was assuming CustomerID is the primary key of your table.

    But the filter should work anyways. What code are you using?
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Apr 2006
    Posts
    17

    Re: Finding row position of a Data Table

    my coding's not the best, but ill post some up now
    Code:
    cboCustomer.Text = _
                dttblLinking.Rows(m_rowPosition)("CustomerID").ToString()
    RowPos = dttblCustomer.DefaultView.RowFilter(cboCustomer.SelectedValue)
    txtName = dttblCustomer.Rows(RowPos)("CustomerName").ToString()

  6. #6
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: Finding row position of a Data Table

    Code:
    dttblCustomer.DefaultView.RowFilter = "CustomerID=" & cboCustomer.SelectedValue
    If dttblCustomer.Rows.Count >0 Then txtName = dttblCustomer.Rows(0)("CustomerName").ToString()
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Apr 2006
    Posts
    17

    Re: Finding row position of a Data Table

    wow, thanks, works a treat, however having a slight issue that i had more than one customer in the combo box, but the filtering seems to leave me with just the Customer that has been selected. Thanks again

  8. #8
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: Finding row position of a Data Table

    Quote Originally Posted by rushy View Post
    wow, thanks, works a treat, however having a slight issue that i had more than one customer in the combo box, but the filtering seems to leave me with just the Customer that has been selected. Thanks again
    You can modify the filter according to your needs.
    It is basically the WHERE part of your SQL statement.

    e.g.
    Code:
    dttblCustomer.DefaultView.RowFilter = "CustomerID=1 OR CustomerID=2 OR CustomerID=3"
    If it would be getting too complex, then it is always better to fill your datatable with only rows of your interest from your database with appropriate SQL query.
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  9. #9

    Thread Starter
    Junior Member
    Join Date
    Apr 2006
    Posts
    17

    Re: Finding row position of a Data Table

    The problem i have is that it doesnt seem to filter down the customers, it will just show the customer in row(0) and therefore doesnt change at all when i change the number in the combo box. This is the reason i was looking for a little bit of help in getting the row position of the customername with customerid = combobox

    your help so fars much appreciated though!
    Last edited by rushy; Feb 27th, 2011 at 02:57 PM.

  10. #10
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: Finding row position of a Data Table

    ok.. I see. You have multiple records with same customer ID?

    The filter will filter out all the customers. But since we are showing only 0th row in the textbox it looks like only one record is slected

    You can try something like this:
    Code:
    For each dr as DataRow in dttblCustomer
    txtName.Text  &= dr("CustomerName")
    Next
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  11. #11
    New Member
    Join Date
    Dec 2013
    Posts
    1

    Red face Re: Finding row position of a Data Table

    Quote Originally Posted by rushy View Post
    The problem i have is that it doesnt seem to filter down the customers, it will just show the customer in row(0) and therefore doesnt change at all when i change the number in the combo box. This is the reason i was looking for a little bit of help in getting the row position of the customername with customerid = combobox

    your help so fars much appreciated though!
    I want to tell you, that in the datasources tab, you make a connection with your database server and from there for example, you can drag the customer ID from the customer table to the CustomerID textbox and all the other fields that you want to link in your textboxes that you want to select the primary key from as your source, and then for example, if you have a related table, under the hierarchy of relations you select the table of the other table that you want to link for example Orders to the combobox, and it will take care of the DisplayMember (this is what you want to show inside the comboxBox) and the ValueMember (This is the CustomerID that you want to save in the other table that you want)

    also for example, if you want to find a member or customer, then in the selected_change event of the comboBox, you write the code to call a Query for the CustomerTableAdapter.FillByCustomerID(CustomerDataTable, ComboBox.SelectedValue) This way it will take you directly to the record that you want to display through the bindingsource that is already automatically link with the previous drag that you performed.

    Hope this works for you, there are many ways of doing this task, also using the datatable.select("CustomerID = '" & comboBox.SelectedValue & "'")

    Good Luck

  12. #12
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: Finding row position of a Data Table

    @toalopez, this thread is 2 years old, and replying to this thread will help no one. The OP probably already has had his problem resolved, or the question is not relevant to him any more. Please avoid re-opening such old threads.
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

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