我的gridview页脚未显示计算列的结果

时间:2013-05-24 18:31:47

标签: asp.net gridview

我的aspx代码是:

 <asp:GridView ID="GridView1" runat="server" AllowPaging="True" 
                   AutoGenerateColumns="False" DataKeyNames="Item_id" 
                   DataSourceID="SqlDataSource2" ShowFooter="True" 
                   onrowdatabound="GridView1_RowDataBound">
                   <Columns>
                       <asp:TemplateField HeaderText="Item_id" InsertVisible="False" 
                           SortExpression="Item_id">
                           <ItemTemplate>
                               <asp:Label ID="Label1" runat="server" Text='<%# Bind("Item_id") %>'></asp:Label>
                           </ItemTemplate>
                           <EditItemTemplate>
                               <asp:Label ID="Label1" runat="server" Text='<%# Bind("Item_id") %>'></asp:Label>
                           </EditItemTemplate>
                           <FooterTemplate>
                                <asp:Button ID="BtnInsert" runat="server" Text="Insert"
                                    Font-Bold="true" BackColor="#0099CC" ForeColor="white" Height="30px" Width="100px" />
                            </FooterTemplate>
                       </asp:TemplateField>
                       <asp:TemplateField HeaderText="Category_name" SortExpression="Category_name">
                           <ItemTemplate>
                               <asp:Label ID="Label2" runat="server" Text='<%# Bind("Category_name") %>'></asp:Label>
                           </ItemTemplate>
                           <EditItemTemplate>
                               <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Category_name") %>'></asp:TextBox>
                           </EditItemTemplate>
                           <FooterTemplate>
                                <asp:TextBox ID="TbC" runat="server" Height="20px" Width="120px" Font-Bold="true"></asp:TextBox>
                            </FooterTemplate>
                            <ItemStyle Width ="100px" />
                       </asp:TemplateField>
                       <asp:TemplateField HeaderText="item_name" SortExpression="item_name">
                           <ItemTemplate>
                               <asp:Label ID="Label3" runat="server" Text='<%# Bind("item_name") %>'></asp:Label>
                           </ItemTemplate>
                           <EditItemTemplate>
                               <asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("item_name") %>'></asp:TextBox>
                           </EditItemTemplate>
                           <FooterTemplate>
                                <asp:TextBox ID="TbN" runat="server" Height="20px" Width="120px" Font-Bold="true"></asp:TextBox>
                            </FooterTemplate>
                            <ItemStyle Width ="100px" />
                       </asp:TemplateField>
                       <asp:TemplateField HeaderText="unit_price" SortExpression="unit_price">
                           <ItemTemplate>
                               <asp:TextBox ID="TbPrice" runat="server" Text='<%# Bind("unit_price") %>'></asp:TextBox>
                           </ItemTemplate>
                           <EditItemTemplate>
                               <asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("unit_price") %>'></asp:TextBox>
                           </EditItemTemplate>

                           <FooterTemplate>
                                <asp:TextBox ID="TbP" runat="server" Height="20px" Width="100px" Font-Bold="true"></asp:TextBox>
                            </FooterTemplate>
                            <ItemStyle Width ="100px" />
                       </asp:TemplateField>
                       <asp:TemplateField HeaderText="QuantitySelected" 
                           SortExpression="QuantitySelected">
                           <ItemTemplate>
                               <asp:Textbox ID="TbQt" runat="server"   Text='<%# Bind("QuantitySelected") %>'></asp:Textbox>
                           </ItemTemplate>
                           <EditItemTemplate>
                               <asp:TextBox ID="TextBox4" runat="server" 
                                   Text='<%# Bind("QuantitySelected") %>'></asp:TextBox>
                           </EditItemTemplate>
                           <FooterTemplate>
                                <asp:TextBox ID="TbQ" runat="server" Height="20px" Width="100px" Font-Bold="true"></asp:TextBox>
                            </FooterTemplate>
                            <ItemStyle Width ="100px" />
                       </asp:TemplateField>
                       <asp:TemplateField HeaderText="Total" SortExpression="Total">
                           <ItemTemplate>
                               <asp:Textbox ID="TbTl" runat="server" Text='<%# Bind("Total") %>'></asp:Textbox>
                           </ItemTemplate>
                           <EditItemTemplate>
                               <asp:TextBox ID="TextBox5" runat="server" Text='<%# Bind("Total") %>'></asp:TextBox>
                           </EditItemTemplate>
                           <FooterTemplate>
                                <asp:TextBox ID="TbT" runat="server" Height="20px" Width="100px" Font-Bold="true"></asp:TextBox>
                            </FooterTemplate>
                            <ItemStyle Width ="100px" />
                       </asp:TemplateField>
                       <asp:TemplateField HeaderText="Discount" SortExpression="Discount">
                           <ItemTemplate>
                               <asp:Textbox ID="TbD" runat="server"   Text='<%# Bind("Discount") %>'></asp:Textbox>
                           </ItemTemplate>
                           <EditItemTemplate>
                               <asp:TextBox ID="TextBox6" runat="server" Text='<%# Bind("Discount") %>'></asp:TextBox>
                           </EditItemTemplate>
                           <FooterTemplate>
                                <asp:TextBox ID="TbD" runat="server" Height="20px" Width="100px" Font-Bold="true"></asp:TextBox>
                            </FooterTemplate>
                            <ItemStyle Width ="100px" />
                       </asp:TemplateField>
                       <asp:TemplateField HeaderText="Grand_total" SortExpression="Grand_total">
                           <ItemTemplate>
                               <asp:Textbox ID="TbGtl" runat="server" Text='<%# Bind("Grand_total") %>'></asp:Textbox>
                           </ItemTemplate>

                           <EditItemTemplate>
                               <asp:TextBox ID="TextBox7" runat="server" Text='<%# Bind("Grand_total") %>'></asp:TextBox>
                           </EditItemTemplate>
                           <FooterTemplate>
                                <asp:TextBox ID="TbG" runat="server" Height="20px" Width="100px" Font-Bold="true"></asp:TextBox>
                            </FooterTemplate>
                            <ItemStyle Width ="100px" />
                       </asp:TemplateField>
                   </Columns>
               </asp:GridView>

我的代码背后是:             public partial class ItemPage:System.Web.UI.Page             {                 protected void Page_Load(object sender,EventArgs e)                 {

protected void btn_Click(object sender, EventArgs e)
        {
            SqlDataSource2.InsertParameters["Category_name"].DefaultValue = ((TextBox)GridView1.FooterRow.FindControl("TbC")).Text;
            SqlDataSource2.InsertParameters["item_name"].DefaultValue = ((TextBox)GridView1.FooterRow.FindControl("TbN")).Text;
            SqlDataSource2.InsertParameters["unit_price"].DefaultValue = ((TextBox)GridView1.FooterRow.FindControl("TbP")).Text;
            SqlDataSource2.InsertParameters["QuantitySelected"].DefaultValue = ((TextBox)GridView1.FooterRow.FindControl("TbQ")).Text;

            SqlDataSource2.InsertParameters["Total"].DefaultValue = ((TextBox)GridView1.FooterRow.FindControl("TbT")).Text;
            SqlDataSource2.InsertParameters["Discount"].DefaultValue = ((TextBox)GridView1.FooterRow.FindControl("TbD")).Text;

            SqlDataSource2.InsertParameters["Grand_total"].DefaultValue = ((TextBox)GridView1.FooterRow.FindControl("TbG")).Text;



        }    




                 double price = 0.0f;
        double quantity = 0.0f;
        double total = 0.0f;
        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                TextBox tb11 = (TextBox)e.Row.FindControl("Tb2");
                TextBox tb12 = (TextBox)e.Row.FindControl("Tb3");
                TextBox tb13 = (TextBox)e.Row.FindControl("TbPrice");
                TextBox tb14 = (TextBox)e.Row.FindControl("TbQt");
                TextBox tb15 = (TextBox)e.Row.FindControl("TbTl");
                TextBox tb16 = (TextBox)e.Row.FindControl("TbDs");
                TextBox tb17 = (TextBox)e.Row.FindControl("TbGtl");

                price = Convert.ToDouble(((TextBox)e.Row.FindControl("TbPrice")).Text);
                quantity = Convert.ToDouble(((TextBox)e.Row.FindControl("TbQt")).Text);


                total = price * quantity;
                //total += total;
                tb15.Text = total.ToString();

            }

            else
            {
                if (e.Row.RowType == DataControlRowType.Footer)
                {



                    TextBox tb1 = (TextBox)e.Row.FindControl("TbC");
                    TextBox tb2 = (TextBox)e.Row.FindControl("TbN");
                    TextBox tb3 = (TextBox)e.Row.FindControl("TbP");
                    TextBox tb4 = (TextBox)e.Row.FindControl("TbQ");
                    TextBox tb5 = (TextBox)e.Row.FindControl("TbT");
                    TextBox tb6 = (TextBox)e.Row.FindControl("TbD");
                    TextBox tb7 = (TextBox)e.Row.FindControl("TbG");


                    tb5.Text = total.ToString();

                }
            }
        }


    }
}

我再次发布了我的编辑代码。问题是,当我运行页面时,我的页脚中有空白文本框。只有“总计”列中的文本框具有值,并且这也是一些模糊值...我必须将值放在“价格”“数量”中,然后单击“插入”按钮将值添加到网格中以及“总计”中的计算值专栏..我仍然被困!请帮助!!

1 个答案:

答案 0 :(得分:0)

你还需要这个:

if (e.Row.RowType == DataControlRowType.DataRow)

你在哪里进行总结。

注意:变量需要在事件处理程序之外声明,否则它们将在每次迭代时归零。

编辑 - 在e.Row.RowType == DataControlRowType.Header

时将求和变量归零

编辑执行以下操作:

double grand_total = 0;

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.Header)
    {
        grand_total = 0;
    }

    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        double price    = Convert.ToDouble(((TextBox)e.Row.FindControl("TbP")).Text);
        double quantity = Convert.ToDouble(((TextBox)e.Row.FindControl("TbQ")).Text);
        double total    = price * quantity ;

        ((TextBox)e.Row.FindControl("TbT")).Text = total.ToString();

        grand_total += total;

    }

    if (e.Row.RowType == DataControlRowType.Footer)
    {
        ((TextBox)e.Row.FindControl("TbT")).Text = grand_total.ToString() ;
    }
}