Performing search on dataset

I am a big novice when it comes to ADO.NET and VB.NET so forgive me if I am making this hard to understand.

I have a dataset that has 100+ rows and 12 columns.  My table is named "DsInventory1.Inventory" within the dataset.

I am looking for a brief coding example or resource to learn how to loop through each row and check to see if a specific cell value for a specified column within that row matches the string returned from a textbox on my form.

If anyone can point me in the right direction, it would be appreciated.

Thanks


Answer this question

Performing search on dataset

  • HopeDreamsComeTrue

    The solution I provided does the same thing.  It will return an array of DataRow objects from the datatable that matches the provided selection string.  In your case, you only have a single row returned so index 0 of the array will contain a datarow object representing the entire row.

    However, for your situation, you may want to create a new Dataview based on your DataTable with the filtering expression that you need:

    Dim myView As New DataView(DsInventory1.Inventory, "ColumnName = '" & TextValue & "'", , DataViewRowState.CurrentRows)

    TextFields.DataSource = myView

  • Arun Narayan

    I tried this:


    Dim foundRow As DataRow = DsInventory1.Inventory.Rows.Find(desiredString)
            Me.BindingContext(DsInventory1, "Inventory").Position = foundRow.ItemArray(0)


    and that didn't work.

    I'm really not sure at all where you're coming from on your suggestion.  There is no "textfields" group and I also don't see a datasource property for any of my text box controls.  I don't see why I need a new data view.  Could you please elaborate

    Thanks.


  • Nestor N

    I have a table called Inventory with records that contain values for about 12 different categories/columns.  On the windows form I a textbox for each column.  I have bound them using the databindings property to the corresponding column in table.  I have a first, previous, next, and last button that allows me to cycle through my records.  I have this running without any problem.

    For instance, this will run just fine: 


    Private Sub btnFirst_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFirst.Click
            Try
                Me.BindingContext(DsInventory1, "Inventory").Position = 0
                DisplayRecordPosition() 
            Catch '...
            End Try
        End Sub


    I just want to add a textbox (txtSearch) and a button (btnSearch) that allows me to enter in a desired primary key and then have the form bind to the position of the record that contains said key.  I am understanding that the code below will return one row and that's exactly what I want.


    Dim foundRow As DataRow = DsInventory1.Inventory.Select("ColumnName = " & SoughtValue.ToString)


    However, I am just not sure how to bind "foundRow" to my form.  I was assuming I could use Me.BindingContext(dataset, table).Position and then assign it to the index of whatever row is found in "foundRow" but I"m not sure how to do that.

    I guess the concept of creating a new dataview to handle one record is beyond my scope of understanding at this time.  I have some ASP.NET books and am kind of learning how to do this out of those as well as a Visual Basic .NET book but that is pretty basic.  If there's a good resource online that would describe something similar, feel free to point me in the right direction.  I haven't found anything too useful at this point.

    Thanks again

  • in-q-ztiv

    Okay, lets summarize for clarity

    This code:

    DsInventory1.Inventory.Select("ColumnName = " & SoughtValue.ToString)

    will return an array of DataRow objects.  Since you are dealing with a primary key, the returned array will only contain one item, so index 0 is your row.

    Erymuzuan posted code to use the Find Method of the DataRowCollection.  I was saying that my suggestion above did the same thing as his.

    I provided a method to create a New DataView simply because I thought it might help you in assigning as a DataSource.  With respect to your textboxes, how are you currently binding to them   Or are you at all

  • BlueLou

    use the DataTable.Rows DataRowCollection.Find method


    DataRow foundRow = myDataTable.Rows.Find(textFind.Text);
    Console.WriteLine(foundRow[0]);// display the first column



  • rw-ral

    Okay, I understand what you're doing now.  Unfortunately, that is a bit of a predicament and I'm not sure how to acquire the index of the returned row.  The only thing I can come up with is a slimy hack that  should work, but is currently untested:

    Dim foundRow As DataRow = DsInventory1.Inventory.Select("ColumnName = " & SoughtValue.ToString)
    Dim i As IList = CType(DsInventory1.Inventory.Rows, System.ComponentModel.IListSource).GetList()
    Me.BindingContext(DsInventory1, "Inventory").Position = i.IndexOf(foundRow)


    Sorry I can't help more, hopefully someone can chime in with a more robust solution.

  • Daniel Chu

    I think this is more along the lines of what I am looking for.  However, I don't necessarily want to display the value of one column of the "foundRow".

    I want something similar to this so that I can bind my text fields on my form to the row I get returned from the find/search


         Dim foundRow As DataRow = DsInventory1.Inventory.Rows.Find(textFind.Text)

         Me.BindingContext(DsInventory1, "Inventory").   = foundRow


    I need to find how to bind my form's text controls to the row I get returned.  If I could get the index of "foundRow" I could then do this:


        Me.BindingContext(DsInventory1, "Inventory").Position  = foundRow.indexofrow


    Hopefully this is a little more clear.  Thanks for your assistance.

  • danrhee

    I guess I typed what I wanted wrong.

    What I want to do is this:

    All the records have a unique value in the specific column I wish to search through (it's the primary key column).  When one enters in that value into the textbox on the form, and presses "Search", I want the entire row to be returned that contains that particular value.

    Let me know if you need anymore clarification on my part.  Thanks for your help.

  • kaja

    I do this to load a row into another form but I don't see why you couldn't do this in the same form.

      Me.Cursor = Windows.Forms.Cursors.WaitCursor()
            CurrentTrackingNumber = Me.DataGrid1(DataGrid1.CurrentRowIndex, 0).ToString()
            selIndex = DataGrid1.CurrentRowIndex

            If IsDBNull(CurrentTrackingNumber) = True Then
                MsgBox("No Tracking Number Selected")
            Else
                Dim checkFrm As Form, Opened As Boolean
                For Each checkFrm In Me.MdiChildren
                    If InStr(checkFrm.Text, CurrentTrackingNumber) Then
                        checkFrm.Focus()
                        Opened = True
                    End If
                Next
                If Opened = False Then
                    TicketLoadSetting = 0
                    Dim frm As New TroubleReport()
                    frm.MdiParent = Me
                    Dim RowDS As DataSet = Me.objTroubleReports.Clone()
                    Dim copyRows() As DataRow = objTroubleReports.Tables("tblTroubleReports").Select("TrackingNumber = " & CurrentTrackingNumber)
                    Dim custTable As DataTable = RowDS.Tables("tblTroubleReports")
                    Dim copyRow As DataRow
                    For Each copyRow In copyRows
                        custTable.ImportRow(copyRow)
                    Next
                    frm.objSelectedTroubleReport.Merge(RowDS, False)
                    frm.Show()
                End If
            End If
            Me.Cursor = Windows.Forms.Cursors.Default()

  • Fabio

    I see what you're trying to do and it's a good idea... But it doesn't work :)

    Thanks for your assistance though.  I appreciate it.

  • Gustavo Valdes

    You should be able to use the Select method on a data table to do what you need:

    DsInventory1.Inventory.Select("ColumnName = " & SoughtValue.ToString)


    that will return an array of DataRow objects.

  • Performing search on dataset