Gridview datacontrolrowtype未被识别为数据行

时间:2015-08-28 15:35:22

标签: asp.net vb.net gridview data-binding rowdatabound

我正在尝试学习如何将总数放在我的gridview上,并且已经搜索了2天来找到我的无法工作的原因。 onrowdatabound事件触发,但它不会将行识别为数据行,它只会拾取页脚(我添加了一个文本框并将值放入以确保我没有丢失它)。我对此非常陌生,我可能做错了很简单。

我的代码隐藏是

Protected Sub WeeklyGridView_RowDataBound(ByVal sender As Object, _
  ByVal e As GridViewRowEventArgs) Handles WeeklyGridView.OnRowDataBound
    Dim appleTotal As Integer = 0
    Dim orangeTotal As Integer = 0
    Dim bananaTotal As Integer = 0
    Dim pearTotal As Integer = 0
    Dim grapeTotal As Integer = 0
    Dim peachTotal As Integer = 0
    Dim cherryTotal As Integer = 0
    Dim pineTotal As Integer = 0
    Dim totalTotal As Integer = 0
    Dim ThedataType As String

    ThedataType = e.Row.RowType.ToString
    TextBox1.Text = ThedataType 'this always shows Footer



        If e.Row.RowType = DataControlRowType.DataRow Then 'this never fires

            appleTotal += Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "Apple"))
            orangeTotal += Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, _
              "Orange"))
            bananaTotal += Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, _
              "Banana"))
            pearTotal += Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, _
              "Pear"))
            grapeTotal += Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, _
              "Grape"))
            peachTotal += Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, _
              "Peach"))
            cherryTotal += Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, _
              "Cherry"))
            pineTotal += Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, _
              "Pine"))
            totalTotal += Convert.ToInt64(DataBinder.Eval(e.Row.DataItem, _
              "TOTAL"))
        ElseIf e.Row.RowType = DataControlRowType.Footer Then 'this always fires
            e.Row.Cells(0).Text = "Totals:"
            ' for the Footer, display the running totals
            e.Row.Cells(3).Text = appleTotal.ToString("g")
            e.Row.Cells(4).Text = orangeTotal.ToString("g")
            e.Row.Cells(5).Text = bananaTotal.ToString("g")
            e.Row.Cells(6).Text = pearTotal.ToString("g")
            e.Row.Cells(7).Text = grapeTotal.ToString("g")
            e.Row.Cells(8).Text = peachTotal.ToString("g")
            e.Row.Cells(9).Text = cherryTotal.ToString("g")
            e.Row.Cells(10).Text = pineTotal.ToString("g")
            e.Row.Cells(11).Text = totalTotal.ToString("g")


            e.Row.Font.Bold = True

        End If

End Sub

gridview会根据用户选择的条件进行更新,并在点击搜索按钮时显示并填充。

搜索按钮的荒谬代码是:(我知道这很糟糕,但我正在尝试)

Protected Sub SearchButton_Click(sender As Object, e As ImageClickEventArgs) Handles SearchButton.Click


    Dim wherecls As String = "trees in ("
    Dim whereFNcls As String = "fruitNumber between ("
    Dim whereString As String = ""
    Dim i As Integer = 0
    Dim selectQry As String = "SELECT cast(trees as varchar(3)) as Trees, MIN(fruitnumber) AS FN_Start, MAX(fruitnumber) AS FN_End, COUNT(CASE WHEN fruitType = 'apple' THEN 1 ELSE NULL END) AS apple, COUNT(CASE WHEN fruitType = 'orange' THEN 1 ELSE NULL END) AS orange, COUNT(CASE WHEN fruitType = 'banana' THEN 1 ELSE NULL END) AS banana, COUNT(CASE WHEN fruitType = 'pear' THEN 1 ELSE NULL END) AS pear, COUNT(CASE WHEN fruitType = 'grape' THEN 1 ELSE NULL END) AS grape, COUNT(CASE WHEN fruitType = 'peach' THEN 1 ELSE NULL END) AS peach, COUNT(CASE WHEN fruitType = 'cherry' THEN 1 ELSE NULL END) AS cherry, COUNT(CASE WHEN fruitType = 'pine' THEN 1 ELSE NULL END) AS pine, COUNT(CASE when dcosg is not null THEN 1 ELSE NULL END) AS Total FROM fruitReport WHERE (orchard = @orchard) and "

    orchardTextBox.Text = orchardDropDown.SelectedValue
    ' check if items selected in both listboxes
    If trees_Listbox.Items.Count > 0 Then
        If fruitminListBox.Items.Count > 0 Then
            'cycle through items in fruitnum listbox to create an "in" clause for sql query
            For Each item As ListItem In trees_Listbox.Items
                whereString += String.Join(",", item) + ", "
            Next
            whereString = Left(whereString, Len(whereString) - 2) + ")"

            selectQry += "(" + wherecls + whereString + ")"
            whereFNcls = "(fruitNumber between "
            For Each itemFNmin As ListItem In fruitminListBox.Items
                'create a "between" clause for the min and max FN values entered by user.
                whereOEcls += itemFNmin.Value + " and " + fruitmaxListBox.Items(i).ToString + ") or (fruitNumber between " '(fruitnumber between number and number) or 
                i += 1
            Next
            'trim off the last text portion of the whereOEcls
            whereOEcls = Left(whereOEcls, Len(whereFNcls) - 25)
            selectQry += " and (" + whereFNcls + ") GROUP BY trees ORDER BY trees"

            fruityData.SelectCommand = selectQry


            WeeklyGridView.Visible = True

        Else
            'see if FN is empty but trees is selected
            For Each item As ListItem In trees_Listbox.Items
                whereString += String.Join(",", item) + ", "
            Next
            whereString = Left(whereString, Len(whereString) - 2)
            selectQry += wherecls + whereString + ") GROUP BY trees ORDER BY trees"

            fruityData.SelectCommand = selectQry
            WeeklyGridView.Visible = True
        End If
    Else
        If fruitminListBox.Items.Count > 0 Then
            'check if trees is empty but FN is selected
            whereFNcls = "(fruitNumber between "

            For Each itemFNmin As ListItem In fruitminListBox.Items
                'create a "between" clause for the min and max FN values entered by user.
                whereFNcls += itemFNmin.Value + " and " + fruitmaxListBox.Items(i).ToString + ") or (fruitNumber between " '(fruitnumber between number and number) or 
                i += 1
            Next
            whereFNcls = Left(whereFNcls, Len(whereFNcls) - 26)
            selectQry += whereFNcls + ") GROUP BY trees ORDER BY trees"

            fruityData.SelectCommand = selectQry
            WeeklyGridView.Visible = True
        Else
            'if both are empty search only on orchard
            selectQry = Left(selectQry, Len(selectQry) - 5) + " group by trees order by trees"

            fruityData.SelectCommand = selectQry
            WeeklyGridView.Visible = True
        End If
    End If


End Sub

最后,我的gridview是......

<asp:GridView ID="WeeklyGridView" runat="server" HorizontalAlign="Center" DataSourceID="fruityData" AutoGenerateColumns="False" BackColor="White" BorderColor="#999999" BorderStyle="None" BorderWidth="1px" CellPadding="3" GridLines="Vertical" Visible="False" ShowFooter="True" ShowHeaderWhenEmpty="True">
            <AlternatingRowStyle BackColor="#DCDCDC" />
            <Columns>

                <asp:BoundField DataField="trees" HeaderText="trees" SortExpression="trees" />
                <asp:BoundField DataField="FN_Start" HeaderText="FN_Start" SortExpression="FN_Start" ReadOnly="True" />
                <asp:BoundField DataField="FN_End" HeaderText="FN_End" SortExpression="FN_End" ReadOnly="True" />
                <asp:BoundField DataField="apple" HeaderText="apple" SortExpression="apple" ReadOnly="True" />
                <asp:BoundField DataField="orange" HeaderText="orange" SortExpression="orange" ReadOnly="True" />
                <asp:BoundField DataField="banana" HeaderText="banana" SortExpression="banana" ReadOnly="True" />
                <asp:BoundField DataField="pear" HeaderText="pear" SortExpression="pear" ReadOnly="True" />
                <asp:BoundField DataField="grape" HeaderText="grape" SortExpression="grape" ReadOnly="True" />
                <asp:BoundField DataField="peach" HeaderText="peach" SortExpression="peach" ReadOnly="True" />
                <asp:BoundField DataField="cherry" HeaderText="cherry" SortExpression="cherry" ReadOnly="True" />
                <asp:BoundField DataField="pine" HeaderText="pine" SortExpression="pine" ReadOnly="True" />
                <asp:BoundField DataField="TOTAL" HeaderText="TOTAL" SortExpression="TOTAL" ReadOnly="True" />
            </Columns>
            <FooterStyle BackColor="#CCCCCC" ForeColor="Black" />

            <HeaderStyle BackColor="#000084" Font-Bold="True" ForeColor="White" />
            <PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
            <RowStyle BackColor="#EEEEEE" ForeColor="Black" />
            <SelectedRowStyle BackColor="#008A8C" Font-Bold="True" ForeColor="White" />
            <SortedAscendingCellStyle BackColor="#F1F1F1" />
            <SortedAscendingHeaderStyle BackColor="#0000A9" />
            <SortedDescendingCellStyle BackColor="#CAC9C9" />
            <SortedDescendingHeaderStyle BackColor="#000065" />
        </asp:GridView>
        <asp:SqlDataSource ID="fruityData" runat="server" ConnectionString="<%$ ConnectionStrings:fruityStuff %>" >
            <SelectParameters>
                <asp:ControlParameter ControlID="orchardTextBox" DefaultValue="theGrove" Name="orchard" PropertyName="Text" Type="String" />
            </SelectParameters>
        </asp:SqlDataSource>

非常感谢任何和所有帮助。

2 个答案:

答案 0 :(得分:0)

在您分配WeeklyGridView.DataBind()后,您无法致电SqlDataSource.SelectCommand。试试这个:

fruityData.SelectCommand = selectQry
fruityData.DataBind()

答案 1 :(得分:0)

我知道这是愚蠢的事。我在事件处理程序中定义了变量。我应该这样做。

Dim appleTotal As Integer = 0
Dim orangeTotal As Integer = 0
Dim bananaTotal As Integer = 0
Dim pearTotal As Integer = 0
Dim grapeTotal As Integer = 0
Dim peachTotal As Integer = 0
Dim cherryTotal As Integer = 0
Dim pineTotal As Integer = 0
Protected Sub WeeklyGridView_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs) Handles WeeklyGridView.OnRowDataBound
相关问题