hollertrek51
asked on
Datatable Select Method and a Listbox - How to filter the dt with listbox items?
My datatable is filled and I have a listbox of part numbers. I'd like to select rows from the datatable that correspond to my part number list only. I'm assuming Datatable select method is the best approach, but I'm not sure where to start.
Thanks,
H
Thanks,
H
Another option is to use the DataView
dTable.DefaultView.RowFilt er = "your filter string"
For i As Integer = 0 to dTable.DefaultView.Count - 1
Messagebox.Show dTable.DefaultView.Item(i) .Item(1)
Next
dTable.DefaultView.RowFilt
For i As Integer = 0 to dTable.DefaultView.Count - 1
Messagebox.Show dTable.DefaultView.Item(i)
Next
ASKER
Hey Wyane,
Are you suggesting a literal array? I have the following code. Do I go from here to a new datatable which then allows me to populate a dataGridView? A dataGridView is my final goal.
Jon
Are you suggesting a literal array? I have the following code. Do I go from here to a new datatable which then allows me to populate a dataGridView? A dataGridView is my final goal.
Jon
PartNumberList.SelectionMode = SelectionMode.MultiExtended
Dim x As Integer
' Loop through all items the ListBox.
For x = 0 To PartNumberList.Items.Count - 1
PartNumberList.SetSelected(x, True)
Next x
Dim dataRows() As DataRow = dtPart.Select("partnum = '" & PartNumberList.SelectedValue & "'")
parts.jpeg
Even though your query is not directed at me, using the DataView would be easier if you want to display the filtered results in the grid. So
dTable.DefaultView.RowFilt er = "your filter string"
datagridview1.DataSource = dTable.DefaultView
dTable.DefaultView.RowFilt
datagridview1.DataSource = dTable.DefaultView
ASKER
Hey Code Cruiser,
Fair enough, but what would my filter string look like? Would it be "PartNumberList.SelectedVa lue" precisely?
Jon
Fair enough, but what would my filter string look like? Would it be "PartNumberList.SelectedVa
Jon
The filter string will be same as the one you were using in select method
dTable.DefaultView.RowFilt er = "partnum = '" & PartNumberList.SelectedVal ue & "'"
dTable.DefaultView.RowFilt
ASKER
So what I'm I missing? I'm generating columns that look like this image.
dtPart.DefaultView.RowFilt er = "partnum = '" & PartNumberList.SelectedVal ue & "'"
dvgOriginalAssy.DataSource = dtPart.DefaultView
If Not dtPart Is Nothing Then dtPart.Dispose()
dtPart = New DataTable
dtPart.Columns.Add("partnu m")
dtPart.Columns.Add("partde scription" )
dtPart.Columns.Add("PartOp r")
dtPart.Columns.Add("mtlpar tnum1")
dtPart.Columns.Add("mtlpar tdescripti on1")
dtPart.Columns.Add("mtlsea rchword1")
dtPart.Columns.Add("partDe signer")
dtPart.Columns.Add("partVe ndor")
dvgOriginalAssy.DataSource = dtPart
dvgOriginalAssy.AutoGenera teColumns = True
dvgOriginalAssy.ReadOnly = True
columns.jpeg
dtPart.DefaultView.RowFilt
dvgOriginalAssy.DataSource
If Not dtPart Is Nothing Then dtPart.Dispose()
dtPart = New DataTable
dtPart.Columns.Add("partnu
dtPart.Columns.Add("partde
dtPart.Columns.Add("PartOp
dtPart.Columns.Add("mtlpar
dtPart.Columns.Add("mtlpar
dtPart.Columns.Add("mtlsea
dtPart.Columns.Add("partDe
dtPart.Columns.Add("partVe
dvgOriginalAssy.DataSource
dvgOriginalAssy.AutoGenera
dvgOriginalAssy.ReadOnly = True
columns.jpeg
Why do you have this code in there?
If Not dtPart Is Nothing Then dtPart.Dispose()
dtPart = New DataTable
dtPart.Columns.Add("partnu m")
dtPart.Columns.Add("partde scription" )
dtPart.Columns.Add("PartOp r")
dtPart.Columns.Add("mtlpar tnum1")
dtPart.Columns.Add("mtlpar tdescripti on1")
dtPart.Columns.Add("mtlsea rchword1")
dtPart.Columns.Add("partDe signer")
dtPart.Columns.Add("partVe ndor")
dvgOriginalAssy.DataSource = dtPart
dvgOriginalAssy.AutoGenera teColumns = True
dvgOriginalAssy.ReadOnly = True
Let me mention that the RowFilter is applied on the datatable AFTER the datatable is configured and populated.
If Not dtPart Is Nothing Then dtPart.Dispose()
dtPart = New DataTable
dtPart.Columns.Add("partnu
dtPart.Columns.Add("partde
dtPart.Columns.Add("PartOp
dtPart.Columns.Add("mtlpar
dtPart.Columns.Add("mtlpar
dtPart.Columns.Add("mtlsea
dtPart.Columns.Add("partDe
dtPart.Columns.Add("partVe
dvgOriginalAssy.DataSource
dvgOriginalAssy.AutoGenera
dvgOriginalAssy.ReadOnly = True
Let me mention that the RowFilter is applied on the datatable AFTER the datatable is configured and populated.
ASKER
Obviously that code is already structured in dtPart. That's gone now, a bad idea.
Here's my SQL string query.
dtPart = New DataTable
daOriginalPart = New System.Data.OleDb.OleDbDat aAdapter(" SELECT partnum, partdescription, PartOpr, mtlpartnum1, mtlpartdescription1, mtlsearchword1, partVendor, partDesigner FROM Part", cnOriginalAssy)
daOriginalPart.Fill(dtPart )
dtPart in filled on the form level. Next comes this:
dtPart.DefaultView.RowFilt er = "partnum = '" & PartNumberList.SelectedVal ue & "'"
dvgOriginalAssy.DataSource = dtPart.DefaultView
Here's my SQL string query.
dtPart = New DataTable
daOriginalPart = New System.Data.OleDb.OleDbDat
daOriginalPart.Fill(dtPart
dtPart in filled on the form level. Next comes this:
dtPart.DefaultView.RowFilt
dvgOriginalAssy.DataSource
And you dont see any rows in the grid right? Does the part numbers match exactly? If you use that partnumber in a query directly, do you get any rows?
Also set a breakpoint and see what is being returned in PartNumberList.SelectedVal ue
ASKER
The part number does match exactly. I use the part number directly in the query and it showed up in the grid. And I can populate the entire dtPart table in the grid. But I get nothing when I use PartNumberList.SelectedVal ue. I tried "partnum = '" & PartNumberList.SelectedVal ue & "'" in a messagebox and I only got the "partnum =" portion.
????? :-(
????? :-(
That is the problem. Try some other property such as SelectedItem
ASKER
SelectedItem works fine - finally. I've tried other properties to get multiple selections, but I can still only do one part from the list at a time. If I select the entire listbox I get nothing in the grid. Anything obvious? I am getting closer.
To load the full grid when all the listbox items are selected, i think this is not the right approach.
ASKER
Could a loop be used to add each selection to the grid?
May be but why not remove the filter if all the filter items are selected?
ASKER
That worked very well my friend. So it seems I can select 1 or all, but nothing in between. Though I could use other types of filters. At any rate, it works and I get it. Thank you for be so patient and sticking with me.
Regards,
Jon
Regards,
Jon
Glad to help :-)
ASKER
The grid view is still showing the entire dtPart datatable. But the list of items is only a portion of dtPart. So I still have to equate the PartNumberList with the default view.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
That would have taken me a long time solve. But it works like a charm. I'm not sure exactly what Partnum IN means. I'm thinking you're creating a string, the parts in the listbox, that you want in dtPart. This is a great learning experience for me.
Thanks again. :-D
Thanks again. :-D
ASKER
CodeCruiser is extremely thorough, knowledgeable, and responsive to my questions. This was a great help to me.
Glad to help :-)
The loop creates the filter string like this
partnum in('part1', 'part2', 'part3'...)
This is similar to partnum='part1' or partnum='part2'...
The loop creates the filter string like this
partnum in('part1', 'part2', 'part3'...)
This is similar to partnum='part1' or partnum='part2'...
Dim dataRows() as DataRow = MyDataTable.Select("FieldN
The returns an array of DataRows where the field 'FieldName' equals the selected value of the listbox.
Wyane