Share via


Get distinct values in a column of datagridview

Question

Wednesday, March 9, 2011 8:27 AM

Hello,

Like "SELECT DISTINCT(Col)" in sql query, how do we perform this in datagridview?

ex.

Colors|

Blue

Red

Red

White

Yellow

Yellow

 

Output:

Blue

Red

White

Yellow

All replies (3)

Wednesday, March 9, 2011 8:53 AM âś…Answered | 1 vote

you can use LINQ:

Dim distinctColors() As String = (From row As DataGridViewRow In DataGridView1.Rows.Cast(Of DataGridViewRow)() _
                     Where Not row.IsNewRow _
                     Select CStr(row.Cells(columnIndex).Value)).Distinct.ToArray

 

thanks for any help


Wednesday, March 9, 2011 10:04 AM

Beside Linq you can use the overloaded ToTable method from the dataview

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

Success
Cor


Thursday, March 10, 2011 5:00 AM

Thank you Paul. OK I'll try it..

 

Good Bless.

 

Charlie