none
ASP.NET中向Gridview添加合计行问题 RRS feed

  • 常规讨论

  • 我想给Gridview添加合计行对下面例子中的销售数量和销售金额列进行统计,以下是Gridview前台设计的代码:

    <asp:GridView ID="GridView1" runat="server"
                        OnRowDataBound="OnRow_Bound" 
                        AutoGenerateColumns="False"
                        cellspacing="1"
                        DataKeyNames="proid"
                        BorderStyle="None"
                        BorderWidth="0px"
                        BackColor="Silver"
                        EmptyDataText="没有查到符合条件的数据!"
                        width="100%"
                        ShowFooter ="true">
                            <Columns>                       
                                <asp:TemplateField HeaderText="序号" HeaderImageUrl="~/images/refresh.gif">
                                    <ItemTemplate>
                                    <asp:Label ID="Label1" runat="server" Text="<%# GridView1.Rows.Count+ 1 %>"></asp:Label>
                                    </ItemTemplate>
                                    <ItemStyle HorizontalAlign="Center" />
                                    <HeaderStyle Width="30px" />
                                </asp:TemplateField>
                                <asp:BoundField DataField="barcode" HeaderText="商品条码">
                                <HeaderStyle ForeColor="Black"  Width="80px"/>
                                        <ItemStyle HorizontalAlign ="Center" />
                                    </asp:BoundField>
                                <asp:BoundField DataField="proid" HeaderText="商品代码">
                                <HeaderStyle ForeColor="Black"  Width="70px"/>
                                        <ItemStyle HorizontalAlign ="center" />
                                    </asp:BoundField>
                                <asp:BoundField DataField="proname" HeaderText="商品名称">
                                <HeaderStyle ForeColor="Black"  Width="150px"/>
                                    <ItemStyle HorizontalAlign ="center" />
                                </asp:BoundField>
                                <asp:BoundField DataField="measureid" HeaderText="单位">
                                <HeaderStyle ForeColor="Black"  Width="30px"/>
                                    </asp:BoundField>
                                <asp:BoundField DataField="supname" HeaderText="供应商">
                                <HeaderStyle ForeColor="Black"  Width="150px"/>
                                    <ItemStyle HorizontalAlign ="center" />
                                </asp:BoundField>
                                <asp:BoundField DataField="saleqty" HeaderText="销售数量"  HtmlEncode ="False" >
                                    <HeaderStyle ForeColor="Black"  Width="80px"/>
                                    <ItemStyle HorizontalAlign ="Center" />
                                </asp:BoundField>
                                <asp:BoundField DataField="saleamt" HeaderText="销售金额"  HtmlEncode ="False" >
                                    <HeaderStyle ForeColor="Black"  Width="120px"/>
                                    <ItemStyle HorizontalAlign ="Center" />
                                </asp:BoundField>
                            </Columns>
                            <RowStyle CssClass="list_gray" />
                            <FooterStyle BackColor="Silver" BorderStyle="None" HorizontalAlign="Right" />
                            <HeaderStyle CssClass="title" />
                            <AlternatingRowStyle BackColor="#EBFBF0" />
                            <EmptyDataRowStyle BackColor="#E0E0E0" Font-Bold="True" ForeColor="Red" />
                        </asp:GridView>

    以下是我后台添加合计行的代码:

    DataRowView drvBasicCountInfor = (DataRowView)e.Row.DataItem;
                    inum = Convert.ToDouble(drvBasicCountInfor[5].ToString());//6是第5列的数字
                    //生成总计
                    //totalNumber += iNumber;
                    totalnum += inum;
                    imoney = Convert.ToDouble(drvBasicCountInfor[6].ToString());//6是第5列的数字
                    //生成总计
                    //totalmoneyNumber += imoneyNumber;
                    totalMoney += imoney;

                    e.Row.Cells[0].Text = "总计:";
                    e.Row.Cells[5].Text = string.Format("{0}", totalnum);
                    e.Row.Cells[6].Text = string.Format("{0:c}", totalMoney);

       从前台设计的列来看第1列下标为0,那销售数量和销售金额的下标应该是6和7,但是这样的话运行是系统提示出错(调试的时候发现销售数量和销售金额的下标分别是5和6).所以我用下标5和6试过,但是显示的情况却是销售数量的合计值在销售数量列下,销售金额的合计值却是在供应商的列下。

       不知道是什么缘故,希望各位能帮忙解答,谢谢!

     

      问题已解决,谢谢!

    2010年3月26日 8:44

全部回复