none
Distinct Data From Gridview

    Question

  • Hi all I load a 47,000 line Text file into a Datagridview, not a problem at all.

    I would like to get the Distinct values for Column1. I have been scratching my head on how to do this, I have not as of yet found an easy way to do this,

    Any pointers would be appreciated.

    Thanks


    Doing my best
    Thursday, December 22, 2011 11:51 PM

Answers

  • Hi Nhaas,

    Welcome to the MSDN Forum.

    What do you mean "get the Distinct values for Column1"?

    Do you mean this :

    column1   column2
    abc           12312
    abc           12345

    You just want to get the value  "abc 12312", right?

    If so, how about the data in text file? Is it sorted? If so, you can read your data like this way

    1. set a temporary variable to store the column1's value.

    2. read a new line, if the column1 value of this new line is the same to the temporary, to to the next line of data.

    3. if not, replace the temporary variable value with new one. And insert this line of data into datagridview.

    4. Keep doing step2 and step3 untill all data has been read.

    I hope this will be helpful.

    Best regards,


    Mike Feng
    MSDN Community Support | Feedback to us
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Friday, December 23, 2011 10:55 AM
    Moderator
  • Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        For I = 0 To DataGridView1.Rows.Count - 1
            MsgBox(DataGridView1.Rows(I).Cells(0).Value.ToString)
        Next
    End Sub
    


    If you want something you've never had, you need to do something you've never done.
    • Marked as answer by nhaas Friday, December 23, 2011 4:13 PM
    Friday, December 23, 2011 12:05 AM
    Moderator
  • I ended up doing something that I should have thought earlier about, brain freeze...

     For I = 0 To DataGridView1.Rows.Count - 1
    
                If Unique <> DataGridView1.Rows(I).Cells(1).Value.ToString Then
                    Me.ComboBox1.Items.Add(DataGridView1.Rows(I).Cells(1).Value.ToString)
                    Unique = DataGridView1.Rows(I).Cells(1).Value.ToString
                End If
    
            Next

    Reading what paul posted - helped me regain some brain cells! Thanks


    Doing my best
    • Marked as answer by nhaas Friday, December 23, 2011 4:13 PM
    Friday, December 23, 2011 4:13 PM

All replies

  • value = datagridview.rows(x).cells(0)
    If you want something you've never had, you need to do something you've never done.
    • Marked as answer by nhaas Friday, December 23, 2011 4:13 PM
    • Unmarked as answer by nhaas Friday, December 23, 2011 4:13 PM
    Thursday, December 22, 2011 11:59 PM
    Moderator
  • Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        For I = 0 To DataGridView1.Rows.Count - 1
            MsgBox(DataGridView1.Rows(I).Cells(0).Value.ToString)
        Next
    End Sub
    


    If you want something you've never had, you need to do something you've never done.
    • Marked as answer by nhaas Friday, December 23, 2011 4:13 PM
    Friday, December 23, 2011 12:05 AM
    Moderator
  • Check out this link

    http://msdn.microsoft.com/en-us/library/wec2b2e6.aspx

     


    Regards Pranabjyoti (If a post answers your query mark the post as answer)
    Friday, December 23, 2011 7:45 AM
  • Hi Nhaas,

    Welcome to the MSDN Forum.

    What do you mean "get the Distinct values for Column1"?

    Do you mean this :

    column1   column2
    abc           12312
    abc           12345

    You just want to get the value  "abc 12312", right?

    If so, how about the data in text file? Is it sorted? If so, you can read your data like this way

    1. set a temporary variable to store the column1's value.

    2. read a new line, if the column1 value of this new line is the same to the temporary, to to the next line of data.

    3. if not, replace the temporary variable value with new one. And insert this line of data into datagridview.

    4. Keep doing step2 and step3 untill all data has been read.

    I hope this will be helpful.

    Best regards,


    Mike Feng
    MSDN Community Support | Feedback to us
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Friday, December 23, 2011 10:55 AM
    Moderator
  • I ended up doing something that I should have thought earlier about, brain freeze...

     For I = 0 To DataGridView1.Rows.Count - 1
    
                If Unique <> DataGridView1.Rows(I).Cells(1).Value.ToString Then
                    Me.ComboBox1.Items.Add(DataGridView1.Rows(I).Cells(1).Value.ToString)
                    Unique = DataGridView1.Rows(I).Cells(1).Value.ToString
                End If
    
            Next

    Reading what paul posted - helped me regain some brain cells! Thanks


    Doing my best
    • Marked as answer by nhaas Friday, December 23, 2011 4:13 PM
    Friday, December 23, 2011 4:13 PM
  • =)   
    If you want something you've never had, you need to do something you've never done.
    Friday, December 23, 2011 7:43 PM
    Moderator