Pregunta

So I have a gridview that display hours employees are scheduled to work. The grid is limited to 12 weeks and displays hours only for a selected project. The grid is a product of a stored procedure that creates a pivot table, with the work weeks as the pivot columns.

I'm adding a tooltip for each cell in the grid that displays the total hours scheduled for each week (showing scheduled hours for any project, not just the selected project). If the employee has hours scheduled for just one project, the tooltip displays as expected. However, when there are mutiple projects, only the last project (last row) of the query makes it into the tooltip.

I can see what's happening is that I'm recreating the strTooltip variable for each row, and that's why I only get the last row displayed. But I can't seem to engineer a clever way to read the data efficiently.

The below is called on the grid's rowdatabound event:

        Using con As New SqlConnection(cnSQLLive)

            con.Open()

            Dim cmd As New SqlCommand()
            cmd.CommandType = CommandType.StoredProcedure
            cmd.CommandText = "spJobForecastingGetEmployeeProjectBreakdown"
            cmd.Connection = con

            cmd.Parameters.Add("@alias", SqlDbType.VarChar)
            cmd.Parameters.Add("@startdate", SqlDbType.DateTime)

            cmd.Parameters("@alias").Value = e.Row.Cells(4).Text
            cmd.Parameters("@startdate").Value = datStartDate

            Dim reader As SqlDataReader = cmd.ExecuteReader()

            Do While reader.Read()
                For i As Integer = 5 To e.Row.Cells.Count - 1

                    Dim strTooltip As String = ""
                    Dim intTotalWeekHours As Integer = 0
                    Dim strWorkWeek As String = GridView1.HeaderRow.Cells(i).Text
                    Dim tempTextboxAs TextBox = DirectCast(e.Row.Cells(i).Controls(0), TextBox)

                    If Not IsDBNull(reader(strWorkWeek)) Then

                        strTooltip += reader(strWorkWeek) & " Hours -- " & reader("Project") & "<br />" & vbCrLf
                        intTotalWeekHours += reader(strWorkWeek)

                    End If

                    strTooltip += "-------------" & "<br />" & intTotalWeekHours & " Total Hours This Week"
                    tempTextbox.ToolTip = strTooltip

                Next

            Loop

        End Using

Thanks in advance!

¿Fue útil?

Solución

I figured this out by creating arrays outside of the loop, like so:

        Dim TooltipArray() As String = {"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", ""}
        Dim TotalHoursArray() As Integer = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}

        Using con As New SqlConnection(cnSQLLive)

            con.Open()

            Dim cmd As New SqlCommand()
            cmd.CommandType = CommandType.StoredProcedure
            cmd.CommandText = "spJobForecastingGetEmployeeProjectBreakdown"
            cmd.Connection = con

            cmd.Parameters.Add("@alias", SqlDbType.VarChar)
            cmd.Parameters.Add("@startdate", SqlDbType.DateTime)

            cmd.Parameters("@alias").Value = e.Row.Cells(4).Text
            cmd.Parameters("@startdate").Value = HiddenFieldDate.Value

            Dim reader As SqlDataReader = cmd.ExecuteReader()

            Do While reader.Read()
                For i As Integer = 5 To e.Row.Cells.Count - 1

                    Dim strTooltip As String = ""
                    Dim intTotalWeekHours As Integer = 0
                    Dim strWorkWeek As String = GridViewProjectEntry.HeaderRow.Cells(i).Text

                    Try

                        If Not IsDBNull(reader(strWorkWeek)) Then
                            If reader("Project") = " - " Then
                                TooltipArray(i) += reader(strWorkWeek) & " Hours -- Department Overhead" & "<br />" & vbCrLf
                            Else
                                TooltipArray(i) += reader(strWorkWeek) & " Hours -- " & reader("Project") & "<br />" & vbCrLf
                            End If

                            TotalHoursArray(i) += reader(strWorkWeek)
                        End If
                    Catch ex As Exception

                    End Try

                Next

            Loop

        End Using

Hope that helps someone else!

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top