Results 1 to 10 of 10

Thread: [RESOLVED] Get values from datagridview, but not duplicates???

  1. #1

    Thread Starter
    Addicted Member tgf-47's Avatar
    Join Date
    Feb 2010
    Location
    CapeTown, South Africa -34.01244,18.337415
    Posts
    209

    Resolved [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:

    Code:
    a
    b
    c
    c
    d
    e
    c
    a
    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:
    Code:
    a
    b
    c
    d
    e
    Is there a way to do this?

  2. #2

  3. #3
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    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:
    1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    2.     Dim dt As New DataTable
    3.     For x As Integer = 1 To DataGridView1.Columns.Count
    4.         dt.Columns.Add()
    5.     Next
    6.     Dim rows As New List(Of DataGridViewRow)
    7.     For Each row As DataGridViewRow In DataGridView1.Rows
    8.         Dim tempRow As DataGridViewRow = row
    9.         If tempRow.Index <> DataGridView1.NewRowIndex Then
    10.             If rows.FindIndex(Function(r As DataGridViewRow) r.Cells(0).Value.ToString = tempRow.Cells(0).Value.ToString) = -1 Then
    11.                 rows.Add(row)
    12.             End If
    13.         End If
    14.     Next
    15.     For Each row In rows
    16.         Dim items() As String = Array.ConvertAll(row.Cells.Cast(Of DataGridViewCell).ToArray, Function(v) v.Value.ToString)
    17.         Dim dr As DataRow = dt.NewRow
    18.         dr.ItemArray = items
    19.         dt.Rows.Add(dr)
    20.     Next
    21. End Sub

  4. #4

    Thread Starter
    Addicted Member tgf-47's Avatar
    Join Date
    Feb 2010
    Location
    CapeTown, South Africa -34.01244,18.337415
    Posts
    209

    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.

  5. #5
    Frenzied Member
    Join Date
    Jan 2010
    Location
    Connecticut
    Posts
    1,687

    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.

  6. #6

    Thread Starter
    Addicted Member tgf-47's Avatar
    Join Date
    Feb 2010
    Location
    CapeTown, South Africa -34.01244,18.337415
    Posts
    209

    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?

  7. #7
    Hyperactive Member
    Join Date
    May 2005
    Posts
    307

    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 :


    I assume that is what you wanted from post #4
    Attached Images Attached Images  
    Last edited by whythetorment; Jul 21st, 2010 at 07:43 AM.

  8. #8
    Frenzied Member
    Join Date
    Jan 2010
    Location
    Connecticut
    Posts
    1,687

    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.

  9. #9

    Thread Starter
    Addicted Member tgf-47's Avatar
    Join Date
    Feb 2010
    Location
    CapeTown, South Africa -34.01244,18.337415
    Posts
    209

    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?
    Name:  calculate.jpg
Views: 1974
Size:  72.0 KB

  10. #10
    Frenzied Member
    Join Date
    Jan 2010
    Location
    Connecticut
    Posts
    1,687

    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
  •  



Click Here to Expand Forum to Full Width