|
-
Jul 21st, 2010, 04:13 AM
#1
Thread Starter
Addicted Member
[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?
-
Jul 21st, 2010, 04:21 AM
#2
Re: Get values from datagridview, but not duplicates???
-
Jul 21st, 2010, 05:08 AM
#3
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
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Jul 21st, 2010, 06:45 AM
#4
Thread Starter
Addicted Member
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.
-
Jul 21st, 2010, 06:55 AM
#5
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.
-
Jul 21st, 2010, 07:16 AM
#6
Thread Starter
Addicted Member
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?
-
Jul 21st, 2010, 07:40 AM
#7
Hyperactive Member
Re: Get values from datagridview, but not duplicates???
Last edited by whythetorment; Jul 21st, 2010 at 07:43 AM.
-
Jul 21st, 2010, 07:54 AM
#8
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.
-
Jul 21st, 2010, 08:20 AM
#9
Thread Starter
Addicted Member
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?
-
Jul 21st, 2010, 08:22 AM
#10
Re: Get values from datagridview, but not duplicates???
Yes use SUM(Units) instead of COUNT(*)
Tags for this Thread
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
|