0

グリッドビューで動的列の小計を追加する方法

region  customer 01-feb-2012 02-feb-2012 ...... 29-feb-2012 Total
chennai xxx               10          20                       30
        yyy               10                                   10
        Total             20          20                       40
mumbai  aaa               20                                   20
        bbb                           10                       10
        Total             20          10                       30

列の数は、選択した月の日付によって異なります。

4

2 に答える 2

0

グリッド ビューで計算する代わりに、データ テーブル レベルで計算し、データ テーブルをグリッド ビューにバインドすることをお勧めします。グリッド ビューで計算すると複雑になるからです。

于 2012-05-28T13:37:19.783 に答える
0

私はあなたのためにサンプルコードを与えました...

コード内でこのコードを参照すると、合計値が動的に計算されます。DataRowBound プロパティを使用すると、その動作...

Aspx コード:

<asp:GridView ID="grdBoardingListReport" runat="server" CellPadding="2" ForeColor="#333333" GridLines="Both" AutoGenerateColumns="false" ShowFooter="false">
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<EditRowStyle BackColor="#999999" />
<FooterStyle BackColor="#c5c5c5" Font-Bold="True" HorizontalAlign="Right" />
<HeaderStyle CssClass="clsReportGridHeader" BackColor="#c5c5c5" Font-Size="10"/>
<PagerStyle BackColor="#39afd9" ForeColor="White" HorizontalAlign="Center"/>
<Columns>
<asp:BoundField DataField="Sl.No" HeaderText="slno" />
<asp:BoundField DataField="fd_name" HeaderText="Name" />
<asp:BoundField DataField="fd_total" HeaderText="Total"/>
</Columns>
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
</asp:GridView>

コードバインド:

private void bindFreePassReport()
{
if (!(Session["FreePassReceive"] == null))
{
DataTable freePassReceive = (DataTable)Session["FreePassReceive"];

if (freePassReceive.Rows.Count > 1)
{
grdFreePassReport.DataSource = freePassReceive;
grdFreePassReport.DataBind();
lblTitle.Text = Session["FreePassReportTitle"].ToString();
}
else
{
grdFreePassReport.DataSource = null;
grdFreePassReport.DataBind();
}
}
}

データ行バウンド:

int Total;
protected void grdBordingPlace_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
total += Convert.ToInt32(e.Row.Cells[2].Text);
}
else if (e.Row.RowType == DataControlRowType.Footer)
{
e.Row.Cells[1].Text = "Total" + " : ";
e.Row.Cells[2].Text = total.ToString();
}
}
于 2012-05-28T17:41:31.577 に答える