Results 1 to 9 of 9

Thread: Search data from a Gridview or Dataset.

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jan 2012
    Posts
    100

    Search data from a Gridview or Dataset.

    Hello Guys,

    I have a datagridview filled from a dataset on the form load, now I want to search specific data from the gridview using a textbox.
    I don't want to go back to database again and fill the gridview.
    I want to search from the existing dataset or Gridview,I guess this is a LINQ method.
    Guys please help me with some advanced code.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Search data from a Gridview or Dataset.

    LINQ didn't exist before .NET 3.5 so obviously this could be done without it. You would traditionally have simply used a loop for this, testing the current row each iteration. LINQ is really just a way to compress loop code into something more succinct. In this case, the LINQ might look something like this:
    Code:
    Dim matchingRow = myDataTable.Rows.Cast(Of DataRow).SingleOrDefault(Function(row) row.Field(Of String)("SomeColumn") = myTextBox.Text)
    That assumes that there will be zero or one matching rows. That can be varied in many ways depending on the requirements.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    Lively Member
    Join Date
    Apr 2011
    Posts
    75

    Re: Search data from a Gridview or Dataset.

    You can loop around the DGV contents (or the original dataset)...

    Code:
    For Each row As DataGridViewRow In DataGridView1.Rows
            MessageBox.Show(row.Cells(0).Value.ToString)
      ' or row.Cells("named column").Value
    Next

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Jan 2012
    Posts
    100

    Re: Search data from a Gridview or Dataset.

    Hello jmcilhinney and geek648,

    Thanks for reply, I guess my question was not in a proper fashion.
    let me clear out this time, I fill gridview on the form load now I want to keep one search text box so when I type some text it will fill
    the gridview related to the text.

    E.g.
    my gridview is loaded with data like
    A123
    A133
    B233
    B344
    so as soon as I enter some text to search like %A1% (it will have a like condition)
    my gridview will show data like
    A123
    A133

    Thats why I was asking for LINQ, I already know the above solution which you have provided.

  5. #5
    Lively Member
    Join Date
    Apr 2011
    Posts
    75

    Re: Search data from a Gridview or Dataset.

    You could have another datatable and when the Search Text changes, copy the matching rows from the original to the shown datatable (or just unhook the DGV from the original dt and manually fill it each time.
    Or set unmatched rows to visible=false in the DGV, if that's possible.

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Search data from a Gridview or Dataset.

    Quote Originally Posted by geek648 View Post
    You could have another datatable and when the Search Text changes, copy the matching rows from the original to the shown datatable (or just unhook the DGV from the original dt and manually fill it each time.
    Or set unmatched rows to visible=false in the DGV, if that's possible.
    Definitely don't do that.

    The proper way to do this is via a BindingSource. Populate your DataTable, bind that to a BindingSource and then bind that to the DataGridView (NOT GridView, which is a different control altogether). To filter the data, simply set the Filter property of the BindingSource, e.g.
    Code:
    myBindingSource.Filter = String.Format("SomeColumn LIKE '{0}%'", myTextBox.Text)
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Jan 2012
    Posts
    100

    Re: Search data from a Gridview or Dataset.

    Hello jmcilhinney,

    You 're simply genius, that resolved and that too with the most effective solution.
    Thanks a lot

  8. #8
    Lively Member
    Join Date
    Apr 2011
    Posts
    75

    Re: Search data from a Gridview or Dataset.

    Quote Originally Posted by jmcilhinney View Post
    Definitely don't do that.

    The proper way to do this is via a BindingSource.
    Well at least I can serve as an example of how not to do it, LOL.
    I'll get my coat.

    Thanks for educating me

  9. #9
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Search data from a Gridview or Dataset.

    Quote Originally Posted by geek648 View Post
    Well at least I can serve as an example of how not to do it, LOL.
    I'll get my coat.

    Thanks for educating me
    Getting it wrong is usually the first step on the road to getting it right.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

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