Results 1 to 8 of 8

Thread: Return the Index value of a datatable, by searching the Datatable

  1. #1

    Thread Starter
    Member
    Join Date
    Nov 2019
    Posts
    44

    Return the Index value of a datatable, by searching the Datatable

    Hi everyone,

    i'm ok with VB but some things are beyond me, and i don't know how to do the following.

    I have a datagridview that displays a short filtered list from my datatable. my datatable is normally around 10,000 rows long.

    i want to change one of the values in my "datagridviews cell" and then update that value in my datatable.

    i have a function that will allow me to update my datatable row.
    Code:
        ''' <summary>
        ''' Updates the data within the datatable
        ''' </summary>
        ''' <param name="TableName">Name of the table</param>
        ''' <param name="index">The index where the data will change</param>
        ''' <param name="value">Values that must match with this datatable</param>
        Public Sub UpdateDataInDataTable(TableName As String, index As Integer, value() As Object)
            Try
                Dim dt = ds.Tables(TableName)
                dt.Rows(index).ItemArray = value
            Catch ex As Exception
                MsgBox(ex.Message)
            End Try
    as you can see this function needs to know the INDEX of which line in the original datatables collection of data to update.

    i can't just use the index of the current row of my datagridview because it might only have say 5 rows and my datatable has 10,000 rows.

    if it was possible to say "search" the datatable for the unique identifier in my datatable and then return what index row its on that would be great but i don't know how to do that.
    my unique identifier is in a column called "Batch_Number". the unique identifier is typically a number like 0013205489 it has the double zeros at the start so i guess it would be treated like a string.

    is anyone able to assist with this please?

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    105,748

    Re: Return the Index value of a datatable, by searching the Datatable

    The Rows collection of a DataTable has a Find method that will find a DataRow by primary key. The index is irrelevant because you'd only be using that to get the row and that Find method returns the row.

  3. #3
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    33,060

    Re: Return the Index value of a datatable, by searching the Datatable

    i want to change one of the values in my "datagridviews cell" and then update that value in my datatable.
    No, that's not what you want to do. What you want to do is simply update your DGV because it should already be bound to the datatable. Since it's already bound, updating one should update the other. If they are not bound, why aren't they?

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  4. #4

    Thread Starter
    Member
    Join Date
    Nov 2019
    Posts
    44

    Re: Return the Index value of a datatable, by searching the Datatable

    Quote Originally Posted by techgnome View Post
    No, that's not what you want to do. What you want to do is simply update your DGV because it should already be bound to the datatable. Since it's already bound, updating one should update the other. If they are not bound, why aren't they?

    -tg
    well i dont really know if they are or are not, i dont have enough experiance with VB and coding to give you that answer,

    DataGridView1.DataSource = dt

    is the code i use when i want to change whats in the DataGridView

    initially the program was always going to just show you search results. but i now want to add a new feature this requires updating the data in the datatable, so if i search the same thing again i can see that change applied.

  5. #5

    Thread Starter
    Member
    Join Date
    Nov 2019
    Posts
    44

    Re: Return the Index value of a datatable, by searching the Datatable

    Quote Originally Posted by jmcilhinney View Post
    The Rows collection of a DataTable has a Find method that will find a DataRow by primary key. The index is irrelevant because you'd only be using that to get the row and that Find method returns the row.
    i know it has a find method, i have no idea how to use it or implement it however.

  6. #6
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    105,748

    Re: Return the Index value of a datatable, by searching the Datatable

    Quote Originally Posted by AndyAndromeda View Post
    i know it has a find method, i have no idea how to use it or implement it however.
    So then read the documentation and search for examples on the web.

  7. #7

    Thread Starter
    Member
    Join Date
    Nov 2019
    Posts
    44

    Re: Return the Index value of a datatable, by searching the Datatable

    Quote Originally Posted by jmcilhinney View Post
    So then read the documentation and search for examples on the web.
    i have, and i can't get an example to work for me, or one that's annotated enough to help make enough sense.

    the documentation also seems to say that i need at least one column to be set at the primary key, while i have a column with unique value i don't have it set at primary key, and again i also don't know how to implement that. i was hoping it would be as simple as 4 to 6 lines of code to return the value i wanted.

  8. #8
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    105,748

    Re: Return the Index value of a datatable, by searching the Datatable

    Quote Originally Posted by AndyAndromeda View Post
    i have a column with unique value i don't have it set at primary key, and again i also don't know how to implement that.
    If only the DataTable had a property dedicated to that and the documentation for that property included a code example. Oh look, it does!

Tags for this Thread

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