[RESOLVED] Get values from datagridview, but not duplicates???
I need to get all the different values from my dgv, but no duplicates, and add them as rows to my dataset.
for example if my dgv col1 have the following values:
the procedure must get the values from dgv1 and add it to a dataset if the values isnt already there.
My dataset should have this:
Is there a way to do this?
Re: Get values from datagridview, but not duplicates???
Re: Get values from datagridview, but not duplicates???
here's an example that takes rows from a dgv where column 0 is distinct + adds those rows to a new datatable:
vb Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim dt As New DataTable
For x As Integer = 1 To DataGridView1.Columns.Count
dt.Columns.Add()
Next
Dim rows As New List(Of DataGridViewRow)
For Each row As DataGridViewRow In DataGridView1.Rows
Dim tempRow As DataGridViewRow = row
If tempRow.Index <> DataGridView1.NewRowIndex Then
If rows.FindIndex(Function(r As DataGridViewRow) r.Cells(0).Value.ToString = tempRow.Cells(0).Value.ToString) = -1 Then
rows.Add(row)
End If
End If
Next
For Each row In rows
Dim items() As String = Array.ConvertAll(row.Cells.Cast(Of DataGridViewCell).ToArray, Function(v) v.Value.ToString)
Dim dr As DataRow = dt.NewRow
dr.ItemArray = items
dt.Rows.Add(dr)
Next
End Sub
Re: Get values from datagridview, but not duplicates???
Ok, I just cant get this to do what I want. I'm hoping you guys can shed some clarity on the issue for me.
What should happen is this:
I have a Dataset containing all rows from my SQL table.
I want to use 4 columns.
1 = Packing (carton, pallet, crate)
2 = Stock (wine, wood, steel)
3 = Year (2001, 2002, 2003)
4 = QTY
I want to populate a 2nd datatable with a "summary" of my stock.
So for each row where column 1, 2 and 3 is exactly the same, it must only add the QTY together and show it as one item.
Re: Get values from datagridview, but not duplicates???
If you're getting your data from a database, then a SQL statement may be your best bet. If you need to pull if from the grid post your code you have so far and tell us what it does wrong and someone will help you with it.
Re: Get values from datagridview, but not duplicates???
Look my code is messed up. I realized I am approaching this from the wrong angle.
Is there a code to search a dataset or a datatable for certain items?
1 Attachment(s)
Re: Get values from datagridview, but not duplicates???
If you getting you're date from Sql, just do a count query.
It will look like this :
Code:
SELECT COUNT(*) as Qty, Packing, Stock, Year
FROM YourTable
Group By Packing, Stock, Year
Then you results will look like :
http://www.vbforums.com/attachment.p...1&d=1279715993
I assume that is what you wanted from post #4
Re: Get values from datagridview, but not duplicates???
whythetorment's SQL is good for a database. You can add the keyword DISTINCT to remove duplicates, but what he has sounds like what you want. Without any examples it is impossible to tell. If you are using a dataset, then you just loop through its members, check thier values and perform the desired actions.
1 Attachment(s)
Re: Get values from datagridview, but not duplicates???
whythetorment. Your sql string works great.
Is there a way to Instead of it counting the number of rows to rather add the values of all 8 those rows together for the UNITS column?
Attachment 79351
Re: Get values from datagridview, but not duplicates???
Yes use SUM(Units) instead of COUNT(*)