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.
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.
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
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.
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.
Re: Search data from a Gridview or Dataset.
Quote:
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)
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 :)
Re: Search data from a Gridview or Dataset.
Quote:
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 :)
Re: Search data from a Gridview or Dataset.
Quote:
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. :)