Click here to Skip to main content
15,888,610 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
dear sir/ma'am


i have a data grid view with values like

coloumn1
1
2
3
1
1
3
5

sir i want the distict count in a text box
such that the answer i want to find as

distinct rows=4

Help me to do this directly from datagridview

sir values are in the datagrid view so how can i get distinct count
Posted
Updated 3-May-13 17:02pm
v2

You could do it with LINQ.
VB
Dim distinctCount As Int32 = From r In dgv1.Rows _
                             Let rw = CType(r, DataGridViewRow) _
                             Where Not rw.IsNewRow _
                             Select rw.Cells(0).Value _
                             Distinct.Count
 
Share this answer
 
v2
You can also specify it in your SQL statement, like this:

VB
Using conn As New SqlConnection(SqlConnString)
    conn.Open()
    Using comm As New SqlCommand("SELECT Distinct [ItemNo] FROM [" & LiveDBName & "].[dbo].[Groups]", conn)
        Using reader As SqlDataReader = comm.ExecuteReader()
            theTable.Load(reader)
            conn.Close()
        End Using
    End Using
End Using
 
Share this answer
 
Hi,

not sure about grid view ,but you can use grid data source to find distinct.

C#
dt = Grid.DataSource;
dt.DefaultView.ToTable(true, new string[] { "Column1" });


this will return rows with distinct values and you can get count from it
 
Share this answer
 
v2
Comments
RaviRanjanKr 5-May-13 14:18pm    
Always wrap your code in pre tag.
Amol_B 6-May-13 0:52am    
Thanks.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900