使用类别计算我的datagridview上显示的数据

时间:2014-02-18 05:59:01

标签: vb.net datagridview

我在使用类别计算datagridview上的数据时遇到了麻烦,即当我通过“部门”过滤我的数据时,它会显示同一类别的所有数据。但我想算一下这个类别(部门)中包含多少员工,主管和经理。我希望它显示在我的文本框上,即txtStaff,tstManagers,txtSups。谢谢你!

这是我目前的代码:

    Try
    Dim conF As New OleDb.OleDbConnection("PROVIDER=Microsoft.ACE.OLEDB.12.0.; Data Source = ..\Database\IRMG TO.accdb")
    conF.Open()
        Dim query As String
        query = "SELECT Employee_Name, P_Level, Position_Title, Department, Team, Subteam1, Subteam2, Immediate_Head, CAREER_BAND, CAREER_ZONE FROM tblEmployees WHERE (" & txtFilterType.Text & " LIKE '%" & txtFilter.Text & "%') "
        Dim command As New OleDb.OleDbCommand(query, conF)
    Dim adapter As New OleDb.OleDbDataAdapter
        Dim dt As New DataTable
    adapter.SelectCommand = command
    adapter.Fill(dt)
    dgvEmployee.DataSource = dt
    adapter.Dispose()
    command.Dispose()
        conF.Close()
    Catch ex As Exception
    End Try

2 个答案:

答案 0 :(得分:0)

试试这个......:

txtResult.Text = dgvEmployee.Rows.Count

答案 1 :(得分:0)

您可能想要执行此SQL语句...

query = "SELECT (Select COUNT(Employee_Name) FROM tblEmployees WHERE Position_Title='Staff' AND (" & txtFilterType.Text & " LIKE '%" & txtFilter.Text & "%')) as StaffCount,
        (Select COUNT(Employee_Name) FROM tblEmployees WHERE Position_Title='Supervisor' AND (" & txtFilterType.Text & " LIKE '%" & txtFilter.Text & "%')) as SupervisorCount,
        (Select COUNT(Employee_Name) FROM tblEmployees WHERE Position_Title='Manager' AND (" & txtFilterType.Text & " LIKE '%" & txtFilter.Text & "%')) as ManagerCount,
         Employee_Name, P_Level, Position_Title, Department, Team, Subteam1, Subteam2, Immediate_Head, CAREER_BAND, CAREER_ZONE FROM tblEmployees WHERE (" & txtFilterType.Text & " LIKE '%" & txtFilter.Text & "%')"

那么VB应该是这样的......

  adapter.Fill(dt)
  txtStaff.Text = dt.Rows(0).Item("StaffCount") 
  txtSups.Text = dt.Rows(0).Item("SupervisorCount") 
  tstManagers.Text = dt.Rows(0).Item("ManagerCount") 

不要忘记在填充后隐藏datagridview的前3列。您正在使用数据源属性,所以一切都会出来,只需隐藏它们。如果您执行循环显示项目而不是直接使用数据源,那么在填充之前,您可以更好地控制数据网格视图。