Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/sql-server-2008/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何仅在数据不为空时显示DataGrid页眉和页脚行(vb.net)_Vb.net_Sql Server 2008_Visual Studio 2008 - Fatal编程技术网

如何仅在数据不为空时显示DataGrid页眉和页脚行(vb.net)

如何仅在数据不为空时显示DataGrid页眉和页脚行(vb.net),vb.net,sql-server-2008,visual-studio-2008,Vb.net,Sql Server 2008,Visual Studio 2008,我已经看到了许多.NET解决方案,介绍了如何在网格不返回数据时显示页眉和/或页脚行。我想做相反的事情,当返回数据时(通过sql存储过程),然后对代码中的每一列进行合计,并在页脚行中显示总计,但将每一行隐藏在页眉和页脚之间。为什么?因为我还没有弄清楚如何(在SQL中)求和,在使用参数时求和,否则我只会在一行中返回最终的和,并在网格中显示一行。更好的解决方案可能是重新编写SQL查询,但由于我对高级SQL的理解有限,我选择了在代码隐藏(不是GridView)中对DataGrid列求和,并在页脚中显示,

我已经看到了许多.NET解决方案,介绍了如何在网格不返回数据时显示页眉和/或页脚行。我想做相反的事情,当返回数据时(通过sql存储过程),然后对代码中的每一列进行合计,并在页脚行中显示总计,但将每一行隐藏在页眉和页脚之间。为什么?因为我还没有弄清楚如何(在SQL中)求和,在使用参数时求和,否则我只会在一行中返回最终的和,并在网格中显示一行。更好的解决方案可能是重新编写SQL查询,但由于我对高级SQL的理解有限,我选择了在代码隐藏(不是GridView)中对DataGrid列求和,并在页脚中显示,同时隐藏除页眉和页脚以外的所有行。

所以我的问题是,是否可以合计DataGrid列,在页脚中显示总计,但隐藏其他数据行

SQL存储过程

    SELECT [EmpID]  
     , EmpName
     , tblCodes.CodeID 
     , [Timecode]
     , SUM(Hours) AS SumTotal 
     , SUM(CASE WHEN tblCodes.CodeID IN (4,6,7,8,9,10,11,12,13,14,15,17,20, 21,22,24,25,26) THEN Hours ELSE 0 end) as HOURS 
     , SUM(CASE WHEN  tblCodes.CodeID IN (5,23) THEN Hours ELSE 0 end) As SBY   
     , SUM(CASE WHEN tblCodes.CodeID IN (1,16)THEN Hours ELSE 0 end) As OT_CT   
     , SUM(CASE WHEN tblCodes.CodeID IN (3)THEN Hours ELSE 0 end) As OOL   
  FROM [TLS].[dbo].[vwTimeSummary]  

  JOIN tblCodes on vwTimeSummary.TimeCode = tblCodes.CodeName

  WHERE DayDate BETWEEN @StartDate  AND @StopDate

  GROUP BY
  ID,Emp, tblCodes.idsCodeID, Timecode

  HAVING
  EmpName = @EmpName
.ASPX Datagrid

<asp:datagrid id="dgdSumHours" runat="server" 
                            Width="250px" 
                            Height="91px" 
                            Font-Names="Tahoma, Arial"
                            Font-Size="Small" 
                            AutoGenerateColumns="False" Caption="Summary of Hours" ShowFooter="True">
                                <AlternatingItemStyle BackColor="#EBF5FF"></AlternatingItemStyle>
                                <headerStyle Font-Bold="True" HorizontalAlign="Center" BackColor="#333333" 
                                    ForeColor="White"></headerStyle>
                 <Columns>
                 <asp:BoundColumn DataField="HOURS" HeaderText="Hours" DataFormatString="{0:#.00}"></asp:BoundColumn>
                 <asp:BoundColumn DataField="SBY" HeaderText="Standby" DataFormatString="{0:#.00}"></asp:BoundColumn>
                 <asp:BoundColumn DataField="OT_CT" HeaderText="Over & Comp time" DataFormatString="{0:#.00}"></asp:BoundColumn>
                 <asp:BoundColumn DataField="OOL" HeaderText="Out of Level" DataFormatString="{0:#.00}"></asp:BoundColumn>

                    </Columns>  
                    </asp:datagrid>
更新:使用CSS样式有一种方法可以隐藏行,并且仍然能够从隐藏的代码访问行中的数据,但是它会在隐藏行所在的页眉行和页脚行之间留下一个间隙。如果数据返回很多行,这是有问题的

应用此CSS样式:

.boundfield-hidden {
   display: none;}
到每个boundcolumns'
ItemStyle CssClassproperty=boundfield hidden


我仍然想知道是否有人有更好的解决方案?

我使用Do while循环来读取DataGrid自定义数据绑定事件*中的SqlDataReader,并将这些值传递给全局变量,从而达到了预期效果。然后在DataGrid的
\u ItemDataBound
事件中,如果行是
.Item
.AlternatingItem
,则跳过例程获取总计,然后直接转到
.Footer
行,从全局变量填充该行中的单元格

修订的项目数据绑定代码:

Protected Sub dgdSumHours_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles dgdSumHours.ItemDataBound

'public variables declared at top of class
'dSumHrs
'dStandbyHrs
'dOOLHrs
'dOT_CTEHrs

If e.Item.ItemType = ListItemType.Item Or _
                           e.Item.ItemType = ListItemType.AlternatingItem Then
    '** comment out this bit of code: *** sum all columns
    'dSumHrs += Convert.ToDecimal(DataBinder.Eval(e.Item.DataItem, "HOURS"))
    'dStandbyHrs += Convert.ToDecimal(DataBinder.Eval(e.Item.DataItem, "SBY"))
    'dOOLHrs += Convert.ToDecimal(DataBinder.Eval(e.Item.DataItem, "OOL"))
    'dOT_CTEHrs += Convert.ToDecimal(DataBinder.Eval(e.Item.DataItem, "OT_CT"))
ElseIf e.Item.ItemType = ListItemType.Footer Then

    e.Item.Cells(0).Text = String.Format("{0:#,##0.00}", dSumHrs)
    e.Item.Cells(1).Text = String.Format("{0:#,##0.00}", dStandbyHrs)
    e.Item.Cells(2).Text = String.Format("{0:#,##0.00}", dOT_CTEHrs)
    e.Item.Cells(3).Text = String.Format("{0:#,##0.00}", dOOLHrs)

    dSumHrs = 0
    dStandbyHrs = 0
    dOOLHrs = 0
    dOT_CTEHrs = 0

    End If

End Sub
'Fill DataGrid, dgdEmpSumHours 
    Sub dgdEmpSumHours_BindData() 
        Try

            'connection
            Using cn As SqlConnection = New SqlConnection(cStr)
                'open connection
                cn.Open()

                'command
                Dim cmd As SqlClient.SqlCommand = New SqlClient.SqlCommand("storedprocedurename", conn)
                cmd.CommandType = CommandType.StoredProcedure

                'pass input parameter
                cmd.Parameters.Add("@EmpName", SqlDbType.VarChar).Value = ddwnEmpList.SelectedValue  
                cmd.Parameters.Add("@BStartDate", SqlDbType.VarChar).Value = PayPeriodBegDate()
                cmd.Parameters.Add("@StopDate", SqlDbType.VarChar).Value = PayPeriodEndDate()

                'execute(Query)
                Dim reader As System.Data.SqlClient.SqlDataReader = cmd.ExecuteReader(CommandBehavior.CloseConnection)

                dgdEmpSumHours.DataSource = reader 

                If (reader.HasRows) Then

                    Do While (reader.Read())

                        If IsDBNull(reader.Item("HOURS")) Then
                            dSumHours = 0
                        Else
                            dSumHours += reader.Item("HOURS")
                        End If
                        '
                        If IsDBNull(reader.Item("SBY")) Then
                            dStandbyHrs = 0
                        Else
                            dStandbyHrs += reader.Item("SBY")
                        End If
                        '
                        If IsDBNull(reader.Item("OTCT")) Then
                            dOT_CTEHrs = 0
                        Else
                            dOT_CTEHrs += reader.Item("OTCT")
                        End If
                        '
                        If IsDBNull(reader.Item("OOL")) Then
                            dOOLHrs = 0
                        Else
                            dOOLHrs += reader.Item("OOL")
                        End If

                    Loop
                Else

                    ScriptManager.RegisterClientScriptBlock(Me, GetType(Page), "showalert", "alert('Oops, there is a problem displaying Summary of Hours table')", True)

                End If

                dgdEmpSumHours.DataBind() 

            End Using

        Catch ex As System.Data.SqlClient.SqlException

            'message about exception here
            ScriptManager.RegisterClientScriptBlock(Me, GetType(Page), "script", "alert('A SQL Exception occurred: ' + ex.Message + '.')", True) 

        End Try

    End Sub
*自定义数据绑定事件:

Protected Sub dgdSumHours_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles dgdSumHours.ItemDataBound

'public variables declared at top of class
'dSumHrs
'dStandbyHrs
'dOOLHrs
'dOT_CTEHrs

If e.Item.ItemType = ListItemType.Item Or _
                           e.Item.ItemType = ListItemType.AlternatingItem Then
    '** comment out this bit of code: *** sum all columns
    'dSumHrs += Convert.ToDecimal(DataBinder.Eval(e.Item.DataItem, "HOURS"))
    'dStandbyHrs += Convert.ToDecimal(DataBinder.Eval(e.Item.DataItem, "SBY"))
    'dOOLHrs += Convert.ToDecimal(DataBinder.Eval(e.Item.DataItem, "OOL"))
    'dOT_CTEHrs += Convert.ToDecimal(DataBinder.Eval(e.Item.DataItem, "OT_CT"))
ElseIf e.Item.ItemType = ListItemType.Footer Then

    e.Item.Cells(0).Text = String.Format("{0:#,##0.00}", dSumHrs)
    e.Item.Cells(1).Text = String.Format("{0:#,##0.00}", dStandbyHrs)
    e.Item.Cells(2).Text = String.Format("{0:#,##0.00}", dOT_CTEHrs)
    e.Item.Cells(3).Text = String.Format("{0:#,##0.00}", dOOLHrs)

    dSumHrs = 0
    dStandbyHrs = 0
    dOOLHrs = 0
    dOT_CTEHrs = 0

    End If

End Sub
'Fill DataGrid, dgdEmpSumHours 
    Sub dgdEmpSumHours_BindData() 
        Try

            'connection
            Using cn As SqlConnection = New SqlConnection(cStr)
                'open connection
                cn.Open()

                'command
                Dim cmd As SqlClient.SqlCommand = New SqlClient.SqlCommand("storedprocedurename", conn)
                cmd.CommandType = CommandType.StoredProcedure

                'pass input parameter
                cmd.Parameters.Add("@EmpName", SqlDbType.VarChar).Value = ddwnEmpList.SelectedValue  
                cmd.Parameters.Add("@BStartDate", SqlDbType.VarChar).Value = PayPeriodBegDate()
                cmd.Parameters.Add("@StopDate", SqlDbType.VarChar).Value = PayPeriodEndDate()

                'execute(Query)
                Dim reader As System.Data.SqlClient.SqlDataReader = cmd.ExecuteReader(CommandBehavior.CloseConnection)

                dgdEmpSumHours.DataSource = reader 

                If (reader.HasRows) Then

                    Do While (reader.Read())

                        If IsDBNull(reader.Item("HOURS")) Then
                            dSumHours = 0
                        Else
                            dSumHours += reader.Item("HOURS")
                        End If
                        '
                        If IsDBNull(reader.Item("SBY")) Then
                            dStandbyHrs = 0
                        Else
                            dStandbyHrs += reader.Item("SBY")
                        End If
                        '
                        If IsDBNull(reader.Item("OTCT")) Then
                            dOT_CTEHrs = 0
                        Else
                            dOT_CTEHrs += reader.Item("OTCT")
                        End If
                        '
                        If IsDBNull(reader.Item("OOL")) Then
                            dOOLHrs = 0
                        Else
                            dOOLHrs += reader.Item("OOL")
                        End If

                    Loop
                Else

                    ScriptManager.RegisterClientScriptBlock(Me, GetType(Page), "showalert", "alert('Oops, there is a problem displaying Summary of Hours table')", True)

                End If

                dgdEmpSumHours.DataBind() 

            End Using

        Catch ex As System.Data.SqlClient.SqlException

            'message about exception here
            ScriptManager.RegisterClientScriptBlock(Me, GetType(Page), "script", "alert('A SQL Exception occurred: ' + ex.Message + '.')", True) 

        End Try

    End Sub

免责声明:在我试图对代码进行一般化的过程中,这个版本的代码中可能存在打字错误。它在我的环境中正常工作。

我使用Do while循环读取DataGrid自定义数据绑定事件*中的SqlDataReader,并将这些值传递给全局变量,从而达到了预期效果。然后在DataGrid的
\u ItemDataBound
事件中,如果行是
.Item
.AlternatingItem
,则跳过例程获取总计,然后直接转到
.Footer
行,从全局变量填充该行中的单元格

修订的项目数据绑定代码:

Protected Sub dgdSumHours_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles dgdSumHours.ItemDataBound

'public variables declared at top of class
'dSumHrs
'dStandbyHrs
'dOOLHrs
'dOT_CTEHrs

If e.Item.ItemType = ListItemType.Item Or _
                           e.Item.ItemType = ListItemType.AlternatingItem Then
    '** comment out this bit of code: *** sum all columns
    'dSumHrs += Convert.ToDecimal(DataBinder.Eval(e.Item.DataItem, "HOURS"))
    'dStandbyHrs += Convert.ToDecimal(DataBinder.Eval(e.Item.DataItem, "SBY"))
    'dOOLHrs += Convert.ToDecimal(DataBinder.Eval(e.Item.DataItem, "OOL"))
    'dOT_CTEHrs += Convert.ToDecimal(DataBinder.Eval(e.Item.DataItem, "OT_CT"))
ElseIf e.Item.ItemType = ListItemType.Footer Then

    e.Item.Cells(0).Text = String.Format("{0:#,##0.00}", dSumHrs)
    e.Item.Cells(1).Text = String.Format("{0:#,##0.00}", dStandbyHrs)
    e.Item.Cells(2).Text = String.Format("{0:#,##0.00}", dOT_CTEHrs)
    e.Item.Cells(3).Text = String.Format("{0:#,##0.00}", dOOLHrs)

    dSumHrs = 0
    dStandbyHrs = 0
    dOOLHrs = 0
    dOT_CTEHrs = 0

    End If

End Sub
'Fill DataGrid, dgdEmpSumHours 
    Sub dgdEmpSumHours_BindData() 
        Try

            'connection
            Using cn As SqlConnection = New SqlConnection(cStr)
                'open connection
                cn.Open()

                'command
                Dim cmd As SqlClient.SqlCommand = New SqlClient.SqlCommand("storedprocedurename", conn)
                cmd.CommandType = CommandType.StoredProcedure

                'pass input parameter
                cmd.Parameters.Add("@EmpName", SqlDbType.VarChar).Value = ddwnEmpList.SelectedValue  
                cmd.Parameters.Add("@BStartDate", SqlDbType.VarChar).Value = PayPeriodBegDate()
                cmd.Parameters.Add("@StopDate", SqlDbType.VarChar).Value = PayPeriodEndDate()

                'execute(Query)
                Dim reader As System.Data.SqlClient.SqlDataReader = cmd.ExecuteReader(CommandBehavior.CloseConnection)

                dgdEmpSumHours.DataSource = reader 

                If (reader.HasRows) Then

                    Do While (reader.Read())

                        If IsDBNull(reader.Item("HOURS")) Then
                            dSumHours = 0
                        Else
                            dSumHours += reader.Item("HOURS")
                        End If
                        '
                        If IsDBNull(reader.Item("SBY")) Then
                            dStandbyHrs = 0
                        Else
                            dStandbyHrs += reader.Item("SBY")
                        End If
                        '
                        If IsDBNull(reader.Item("OTCT")) Then
                            dOT_CTEHrs = 0
                        Else
                            dOT_CTEHrs += reader.Item("OTCT")
                        End If
                        '
                        If IsDBNull(reader.Item("OOL")) Then
                            dOOLHrs = 0
                        Else
                            dOOLHrs += reader.Item("OOL")
                        End If

                    Loop
                Else

                    ScriptManager.RegisterClientScriptBlock(Me, GetType(Page), "showalert", "alert('Oops, there is a problem displaying Summary of Hours table')", True)

                End If

                dgdEmpSumHours.DataBind() 

            End Using

        Catch ex As System.Data.SqlClient.SqlException

            'message about exception here
            ScriptManager.RegisterClientScriptBlock(Me, GetType(Page), "script", "alert('A SQL Exception occurred: ' + ex.Message + '.')", True) 

        End Try

    End Sub
*自定义数据绑定事件:

Protected Sub dgdSumHours_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles dgdSumHours.ItemDataBound

'public variables declared at top of class
'dSumHrs
'dStandbyHrs
'dOOLHrs
'dOT_CTEHrs

If e.Item.ItemType = ListItemType.Item Or _
                           e.Item.ItemType = ListItemType.AlternatingItem Then
    '** comment out this bit of code: *** sum all columns
    'dSumHrs += Convert.ToDecimal(DataBinder.Eval(e.Item.DataItem, "HOURS"))
    'dStandbyHrs += Convert.ToDecimal(DataBinder.Eval(e.Item.DataItem, "SBY"))
    'dOOLHrs += Convert.ToDecimal(DataBinder.Eval(e.Item.DataItem, "OOL"))
    'dOT_CTEHrs += Convert.ToDecimal(DataBinder.Eval(e.Item.DataItem, "OT_CT"))
ElseIf e.Item.ItemType = ListItemType.Footer Then

    e.Item.Cells(0).Text = String.Format("{0:#,##0.00}", dSumHrs)
    e.Item.Cells(1).Text = String.Format("{0:#,##0.00}", dStandbyHrs)
    e.Item.Cells(2).Text = String.Format("{0:#,##0.00}", dOT_CTEHrs)
    e.Item.Cells(3).Text = String.Format("{0:#,##0.00}", dOOLHrs)

    dSumHrs = 0
    dStandbyHrs = 0
    dOOLHrs = 0
    dOT_CTEHrs = 0

    End If

End Sub
'Fill DataGrid, dgdEmpSumHours 
    Sub dgdEmpSumHours_BindData() 
        Try

            'connection
            Using cn As SqlConnection = New SqlConnection(cStr)
                'open connection
                cn.Open()

                'command
                Dim cmd As SqlClient.SqlCommand = New SqlClient.SqlCommand("storedprocedurename", conn)
                cmd.CommandType = CommandType.StoredProcedure

                'pass input parameter
                cmd.Parameters.Add("@EmpName", SqlDbType.VarChar).Value = ddwnEmpList.SelectedValue  
                cmd.Parameters.Add("@BStartDate", SqlDbType.VarChar).Value = PayPeriodBegDate()
                cmd.Parameters.Add("@StopDate", SqlDbType.VarChar).Value = PayPeriodEndDate()

                'execute(Query)
                Dim reader As System.Data.SqlClient.SqlDataReader = cmd.ExecuteReader(CommandBehavior.CloseConnection)

                dgdEmpSumHours.DataSource = reader 

                If (reader.HasRows) Then

                    Do While (reader.Read())

                        If IsDBNull(reader.Item("HOURS")) Then
                            dSumHours = 0
                        Else
                            dSumHours += reader.Item("HOURS")
                        End If
                        '
                        If IsDBNull(reader.Item("SBY")) Then
                            dStandbyHrs = 0
                        Else
                            dStandbyHrs += reader.Item("SBY")
                        End If
                        '
                        If IsDBNull(reader.Item("OTCT")) Then
                            dOT_CTEHrs = 0
                        Else
                            dOT_CTEHrs += reader.Item("OTCT")
                        End If
                        '
                        If IsDBNull(reader.Item("OOL")) Then
                            dOOLHrs = 0
                        Else
                            dOOLHrs += reader.Item("OOL")
                        End If

                    Loop
                Else

                    ScriptManager.RegisterClientScriptBlock(Me, GetType(Page), "showalert", "alert('Oops, there is a problem displaying Summary of Hours table')", True)

                End If

                dgdEmpSumHours.DataBind() 

            End Using

        Catch ex As System.Data.SqlClient.SqlException

            'message about exception here
            ScriptManager.RegisterClientScriptBlock(Me, GetType(Page), "script", "alert('A SQL Exception occurred: ' + ex.Message + '.')", True) 

        End Try

    End Sub
免责声明:在我试图对代码进行一般化的过程中,这个版本的代码中可能存在打字错误。它在我的环境中正常工作