获取不同的值 [英] Fetch Distinct values

查看:68
本文介绍了获取不同的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的先生/女士





i有一个数据网格视图,其值为



coloumn1

1

2

3

1

1

3

5



先生,我希望文本框中的分心数量

这样的答案我想找到



不同的行= 4



帮我直接从datagridview执行此操作



sir值在datagrid视图中,所以我怎样才能得到不同的计数

解决方案





不确定网格视图,但你可以使用网格数据来查找不同的内容。



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





这将返回具有不同值的行,您可以从中获取计数


您也可以在SQL语句中指定它,像这样:



 使用 conn  As   SqlConnection(SqlConnString)
conn.Open()
使用 comm 作为 SqlCommand( SELECT Distinct [ItemNo] FROM [& LiveDBName& ]。[dbo]。[Groups],conn)
使用 reader As SqlDataReader = comm.ExecuteReader()
theTable.Load(reader)
conn.Close()
结束 使用
结束 使用
结束 使用


你可以用LINQ来完成。

  Dim  distinctCount 作为  Int32  =来自r  dgv1.Rows _ 
rw = CType (r,DataGridViewRow)_
其中 rw.IsNewRow _
选择 rw.Cells( 0 )。值_
Distinct.Count


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

解决方案

Hi,

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

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



this will return rows with distinct values and you can get count from it


You can also specify it in your SQL statement, like this:

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


You could do it with LINQ.

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


这篇关于获取不同的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆