10 Replies - 31312 Views - Last Post: 28 March 2013 - 02:36 AM Rate Topic: -----

#1 Adqusit  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 158
  • Joined: 02-March 13

how to filter data in DataGridView

Posted 21 March 2013 - 08:26 AM

Hi. few days a ago, i visited a friend, who was working on an Oracle project, in which he had a helping form, which was to be appeared on the leave event of a textbox, from which a country was just selected by enter the first letter of that country, lets say if he had to select England so he would write E and so all countries starting with E were appeared and then pressing N so all related countries of EN were appeared. right.

Now i have the same type of helping form with DGV, from which i have to select the product name, with same way of textbox. Please guide me that how to do this.

I hope my idea would be clear?

Is This A Good Question/Topic? 0
  • +

Replies To: how to filter data in DataGridView

#2 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1623
  • View blog
  • Posts: 5,200
  • Joined: 25-September 09

Re: how to filter data in DataGridView

Posted 21 March 2013 - 08:33 AM

From what I've seen on how you are filling your datagridviews, I would say that you would need to pass an sql statement with a where clause using LIKE and clear and refill your DGV.

You could also populate your datagrid with a datasource and filter that datasource from the input. (a lot less code)

You might check out the LIKE operator in SQL for more information on how it returns rows from your criteria.

This post has been edited by CharlieMay: 21 March 2013 - 08:51 AM

Was This Post Helpful? 0
  • +
  • -

#3 Adqusit  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 158
  • Joined: 02-March 13

Re: how to filter data in DataGridView

Posted 22 March 2013 - 11:15 AM

Hi. let me clarify myself. Do you mean that whatever i will write in textbox it will be martched with dataBase, by query, and only the related records will be fetched? Am i right?
Was This Post Helpful? 0
  • +
  • -

#4 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1623
  • View blog
  • Posts: 5,200
  • Joined: 25-September 09

Re: how to filter data in DataGridView

Posted 22 March 2013 - 11:27 AM

Yes, that's totally possible.
Was This Post Helpful? 0
  • +
  • -

#5 Adqusit  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 158
  • Joined: 02-March 13

Re: how to filter data in DataGridView

Posted 25 March 2013 - 09:23 AM

So. What i have done is i used a combobox and a button and DataGridView on my form. I set the combobox property AutoCompleteMode = SuggestAppend, AutoCompleteSource = ListItems and DropDown Style = Simple and on the form load event, i used this code, to making the combobox enabled as AutoComplete.
 Dim sel As String = "select * from ProdInfo"
        Dim cmdSql As New SqlCommand(sel, cnSql)
        cmdSql.CommandType = CommandType.Text
        daSql.SelectCommand = cmdSql

        daSql.Fill(dsSql, "ProdInfo")

        Dim a As Integer
        For a = 0 To dsSql.Tables("ProdInfo").Rows.Count - 1
            ComboBox1.Items.Add(dsSql.Tables("ProdInfo").Rows(a).Item(1).ToString)
        Next


and on the click event of the button i used this code.

Dim sel As String = "select * from ProdInfo where Prod_Name = '" & Trim(ComboBox1.Text) & "'"
        Dim cmdSql As New SqlCommand(sel, cnSql)
        cmdSql.CommandType = CommandType.Text
        daSql.SelectCommand = cmdSql

        daSql.Fill(dsSql, "ProdInfo")

        dgvSalesPicker.DataSource = dsSql
        dgvSalesPicker.DataMember = "ProdInfo"


As i write anything in combobox, if it is available in its list, so it appears in the list and i select it right. Now the problem is that when i write something that is available in combobox and press the button (to load that product into DGV), so the selected product is not loaded in the DGV?

Please guide me that why its not working.
Was This Post Helpful? 0
  • +
  • -

#6 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1623
  • View blog
  • Posts: 5,200
  • Joined: 25-September 09

Re: how to filter data in DataGridView

Posted 25 March 2013 - 09:39 AM

Well, dsSql appears to be a dataset. Have you tried setting the datasource of the DGVSalesPicker to the datable of the datasource? Much like you did in the combobox above.

Ignore that, I misread your code. :(

This post has been edited by CharlieMay: 25 March 2013 - 09:44 AM

Was This Post Helpful? 0
  • +
  • -

#7 _HAWK_  Icon User is offline

  • Master(Of Foo)
  • member icon

Reputation: 1067
  • View blog
  • Posts: 4,176
  • Joined: 02-July 08

Re: how to filter data in DataGridView

Posted 25 March 2013 - 11:46 AM

Load your dataset, make queries against it. Set the DataSource for the DGV to Nothing between queries.

LINQ query:

dgv.DataSource = Nothing
'dr is a datarow from the Rows collection
Dim query = Dataset1.Tables(0).Rows.Where(Function(dr) dr("column name").ToString <> "some filter").ToArray()
dgv.DataSource = query

Was This Post Helpful? 0
  • +
  • -

#8 andrewsw  Icon User is offline

  • It's just been revoked!
  • member icon

Reputation: 3836
  • View blog
  • Posts: 13,583
  • Joined: 12-December 12

Re: how to filter data in DataGridView

Posted 25 March 2013 - 01:43 PM

I suspect the following would also work but am unable to test at the moment:

        Dim dt As New DataTable()
        dt = TryCast(dgvSalesPicker.DataSource, DataTable)
        If dt IsNot Nothing Then
            dt.DefaultView.RowFilter = String.Format("Prod_Name = '{0}'", Trim(ComboBox1.Text))
        End If

Was This Post Helpful? 1
  • +
  • -

#9 Adqusit  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 158
  • Joined: 02-March 13

Re: how to filter data in DataGridView

Posted 27 March 2013 - 02:43 AM

I'm afraid to say that i didn't understand the meaning of both the last posted answers. because neither i worked ever with LINQ, not i picked up the idea for both of the posts.
Was This Post Helpful? 0
  • +
  • -

#10 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1623
  • View blog
  • Posts: 5,200
  • Joined: 25-September 09

Re: how to filter data in DataGridView

Posted 27 March 2013 - 05:02 AM

Adqusit,
Did you try either of them? andrewsw's post isn't actually LINQ and is a method supplied by the datatable to filter your defaultview. It is also in my opinion complete code against your current object names.

Or are you saying it works but you don't understand how?
Was This Post Helpful? 0
  • +
  • -

#11 Adqusit  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 158
  • Joined: 02-March 13

Re: how to filter data in DataGridView

Posted 28 March 2013 - 02:36 AM

No I din't try this, because i don't know where to put it and what to do with this. Therefore, I everyday try to find that what to do with this code.



What i have done is that in my Post # 5, the code had a problem, which is mentioned there, that it was not loading the combobox value into DGV. Actually the problem was with DataSet. I was using same dataset for jobs. I just changed the DataSet and its working finely. If a product is available in combobox so it will loaded into DGV by using the button clicking. Its all right now, But i was expecting is not this.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1