如何在网格视图中基于相同的DCNo对单元格值求和? [英] How to sum Cell values based On same DCNo in Grid View?
本文介绍了如何在网格视图中基于相同的DCNo对单元格值求和?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个网格视图,如
I have a gridview like
-----------------------------------------
| Sno | Amount | DCNo |
-----------------------------------------
| 1 | 1500 | 888 |
| 2 | 1000 | 888 |
| 3 | 2500 | 666 |
| 4 | 1500 | 666 |
-----------------------------------------
现在我想显示这样的输出..我的输出基于,每当 DCNo
相同时它就会计算出来总金额。然后它将合并单元格。
Now I want to display output like this.. My out put based on ,whenever DCNo
same then it calculate the total amount.Then it will be merged cells.
----------------------------------------
| Sno | Amount | DCNo |
----------------------------------------
| 1 | | |
| 2 | 2500 | 888 |
|---------------------------------------
| 3 | 4000 | |
| 4 | | 666 |
---------------------------------------
我试过代码Mergining cell
I tried code Mergining cell
protected void GVDCNoConfirm_RowDataBound(object sender, GridViewRowEventArgs e)
{
for (int rowIndex = GVDCNoConfirm.Rows.Count - 2; rowIndex >= 0; rowIndex--)
{
GridViewRow gvRow = GVDCNoConfirm.Rows[rowIndex];
GridViewRow gvPreviousRow = GVDCNoConfirm.Rows[rowIndex + 1];
for (int cellCount = 0; cellCount < gvRow.Cells.Count;
cellCount++)
{
if (gvRow.Cells[cellCount].Text ==
gvPreviousRow.Cells[cellCount].Text)
{
if (gvPreviousRow.Cells[cellCount].RowSpan < 2)
{
gvRow.Cells[cellCount].RowSpan = 2;
}
else
{
gvRow.Cells[cellCount].RowSpan =
gvPreviousRow.Cells[cellCount].RowSpan + 1;
}
gvPreviousRow.Cells[cellCount].Visible = false;
}
}
}
}
现在我想要显示带有合并单元格总和的输出,如第二个网格..
我怎么能得到这个?
Now i want to display output with merged cell sum like second grid..
How can i achive this?
推荐答案
我有另一种方式,你可以将第二个网格的Sno列的htmlencoding属性设置为false并使用br标签
您的第二个网格代码
I have another way, you can set the htmlencoding property of Sno column of your second grid to false and use the br tag
Your second grid code
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:BoundField DataField="Sno" HeaderText="Sno" HtmlEncode="False" />
<asp:BoundField DataField="Amount" HeaderText="Amount" />
<asp:BoundField DataField="DcNo" HeaderText="DcNo" />
</Columns>
</asp:GridView>
绑定网格的代码
code to bind the grid
//Assume dtblSource is the datasource of your first grid
//Declare a data table for grouping
DataTable dtblGroup = new DataTable();
dtblGroup.Columns.Add("Sno", typeof(string));
dtblGroup.Columns.Add("Amount", typeof(decimal));
dtblGroup.Columns.Add("DcNo", typeof(int));
//Get distinct DC nos to a datatable
DataView dv = new DataView(dtblSource);
DataTable dtblDistinct = dv.ToTable(true, "DcNo");
string Sno;
decimal amount;
int dcno;
//loop through distinct dc nos
foreach (DataRow drDist in dtblDistinct.Rows)
{
Sno = string.Empty;
amount = 0;
dcno = (int)drDist["DcNo"];
DataRow[] drws = dtblSource.Select("DcNo=" + dcno.ToString());
//Append slno, and sum amount
foreach (DataRow dr in drws)
{
amount += (decimal)dr["Amount"];
Sno = Sno + (Sno == string.Empty ? "" : "<br>") + dr["Sno"].ToString();
}
dtblGroup.Rows.Add(new object[] { Sno, amount, dcno });
}
GridView1.DataSource = dtblGroup;
GridView1.DataBind();
这篇关于如何在网格视图中基于相同的DCNo对单元格值求和?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文