|
-
Feb 17th, 2013, 01:19 AM
#1
Thread Starter
Lively Member
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.
-
Feb 17th, 2013, 01:35 AM
#2
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.
-
Feb 17th, 2013, 06:26 AM
#3
Lively Member
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
-
Feb 17th, 2013, 11:56 AM
#4
Thread Starter
Lively Member
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.
-
Feb 17th, 2013, 02:00 PM
#5
Lively Member
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.
-
Feb 17th, 2013, 06:20 PM
#6
Re: Search data from a Gridview or Dataset.
 Originally Posted by geek648
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)
-
Feb 17th, 2013, 09:55 PM
#7
Thread Starter
Lively Member
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
-
Feb 18th, 2013, 02:52 AM
#8
Lively Member
Re: Search data from a Gridview or Dataset.
 Originally Posted by jmcilhinney
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
-
Feb 18th, 2013, 08:14 AM
#9
Re: Search data from a Gridview or Dataset.
 Originally Posted by geek648
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|