如何找到viewstate中动态gridview列的总和 [英] How can I find the total sum of dynamic gridview column which is in viewstate
问题描述
在我的销售发票页面中,我有一个gridview,用户可以动态添加项目,数量和价格。我想在gridview旁边显示添加项目的总计。怎么可能?
< asp:GridView ID = GridView1 OnDataBound = GridView1_DataBound runat = server AutoGenerateColumns = < span class =code-string> False BackColor = 白色
BorderColor = #CCCCCC BorderStyle = 实体 BorderWidth = 1px CellPadding = 4 PageSize = 50
字体名称= Arial Narrow >
<色谱柱与GT;
< asp:TemplateField HeaderText = NO >
< ItemTemplate>
<%#Container.DataItemIndex +1 %>
< / ItemTemplate >
< / asp:TemplateField >
< asp:TemplateField HeaderText = 项目名称 >
< ; ItemTemplate中>
< asp:标签ID = Label1 CssClass = CLMNitem runat = server Text = ' <%#Bind([ITEM NAME])%> ;' > < / asp :标签 >
< / ItemTemplate >
< / < span class =code-leadattribute> asp:TemplateField >
< asp:BoundField DataField = PART NO: HeaderText = PART NO: />
< asp:BoundField DataField = UNIT HeaderText = UNIT />
< asp:BoundField DataField = QTY HeaderText = QTY />
< asp:BoundField DataField = PRICE HeaderText = PRICE />
< asp:BoundFie< pre lang = c# >
ld DataField =DISCHeaderText =DISC/>
< asp:TemplateField HeaderText =AMOUNT> ;
< itemtemplate>
< asp:Label ID =Label2runat =serverFont-Bold =trueForeColor =greenText = '<%#Bind(AMOUNT)%>'>
< ; asp:TemplateField HeaderText =CANCEL>
< itemtemplate>
< asp:ImageButton ID =ImageButton2OnClick =ImageButton2_Clickrunat =server ImageAlign =AbsMiddle
ImageUrl =〜/ Scripts / 14612399 82_close_blue.png/>
< HeaderStyle BackColor =#01A5FFFont-Bold =FalseForeColor =WhiteBorderStyle =Solid
BorderWidth =1pxHeight =30pxHorizontalAlign =中心VerticalAlign =中间/>
< pagerstyle backcolor =#99CCCCforecolor =#003399horizontalalign =Left>
< rowstyle forecolor =#003399borderstyle =Solidborderwidth =1pxhorizontalalign =Center>
VerticalAlign =Middle/>
< SelectedRowStyle BackColor =#009999Font-Bold =TrueForeColor =#CCFF99/>
< sortedascendingcellstyle backcolor =#EDF6F6>
< sortedascendinghe aderstyle backcolor =#0D4AC4>
< sorteddescendingcellstyle backcolor =#D6DFDF>
< sorteddescendingheaderstyle backcolor =#002876>
和代码背后是
< pre lang =c#> private void AddNewRecordRowToGrid()
{
// 检查视图状态不为空
if (ViewState [ invoice ]!= null )
{
// 从视图状态获取数据
DataTable dtCurrentTable =(DataTable)ViewState [ 发票跨度>];
DataRow drCurrentRow = null ;
if (dtCurrentTable.Rows.Count > 0 )
{
for ( int i = 1 ; i < = dtCurrentTable.Rows.Count ; i ++)
{
int UserQty = Convert.ToInt32(TextBox2.Text);
int UserPrice = Convert.ToInt32(TextBox3.Text);
int UserDisc;
if (TextBox4.Text == null || TextBox4.Text == )
{
UserDisc = 0 ;
}
else
{
UserDisc = Convert.ToInt32(TextBox4.Text);
}
int linetotal =(UserQty * UserPrice) - (UserQty * UserDisc);
// 将每一行添加到数据表中
drCurrentRow = dtCurrentTable.NewRow();
drCurrentRow [ 项目名称] = TextBox1.Text;
drCurrentRow [ QTY] = TextBox2.Text;
drCurrentRow [ PRICE] = TextBox3.Text;
drCurrentRow [ DISC] = UserDisc;
drCurrentRow [ AMOUNT] = linetotal;
}
// 删除初始空白行
if (dtCurrentTable.Rows [ 0 ] [ 0 ]。ToString()== )
{
dtCurrentTable.Rows [ 0 ]。删除();
dtCurrentTable.AcceptChanges();
}
// 将创建的行添加到dataTable中
dtCurrentTable.Rows.Add(drCurrentRow);
// 创建每一行后将数据表保存到视图状态
ViewState [ invoice] = dtCurrentTable;
// 使用最新行绑定Gridview
GridView1.DataSource = dtCurrentTable;
GridView1.DataBind();
Con.Close();
TextBox1.Text = ;
TextBox2.Text = ;
TextBox3.Text = ;
TextBox4.Text = ;
TextBox1.Focus();
}
< pre lang = c# > for ( int i = 0 ; i < GridView1.Rows.Count; i ++)
{
sum + = int .Parse(GridView1.Rows [i] .Cells [ 7 ]。文本);
GrandTotal.Text = sum.ToString();
}
}
}
我的尝试:
for(int i = 0; i< GridView1.Rows.Count; i ++)
{
sum + = int.Parse(GridView1.Rows [i]。 Cells [7] .Text);
GrandTotal.Text = sum.ToString();
} 试试这个
foreach (DataRow行 in dtCurrentTable.Rows)
{
int UserQty = 0 ,UserPrice = 0 ,UserDisc = 0 ;
int .TryParse(row [ UserQty_ColumnName ] + , out UserQty);
int .TryParse(row [ UserPrice_ColumnName ] + , out UserPrice);
int .TryParse(row [ UserDisc_ColumnName ] + , out UserDisc);
int linetotal =(UserQty * UserPrice) - (UserQty * UserDisc);
sum + = linetotal;
}
GrandTotal.Text = sum.ToString();
GridView1.DataSource = dtCurrentTable;
GridView1.DataBind();
In my sales invoice page i have a gridview where user can add items,Quantity and price dynamically. i want to display the grand total of added items out side the gridview. how it possible ?
<asp:GridView ID="GridView1" OnDataBound="GridView1_DataBound" runat="server" AutoGenerateColumns="False" BackColor="White"
BorderColor="#CCCCCC" BorderStyle="Solid" BorderWidth="1px" CellPadding="4" PageSize="50"
Font-Names="Arial Narrow">
<Columns>
<asp:TemplateField HeaderText="NO">
<ItemTemplate>
<%#Container.DataItemIndex +1 %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="ITEM NAME">
<ItemTemplate>
<asp:Label ID="Label1" CssClass="CLMNitem" runat="server" Text='<%# Bind("[ITEM NAME]") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="PART NO:" HeaderText="PART NO:" />
<asp:BoundField DataField="UNIT" HeaderText="UNIT" />
<asp:BoundField DataField="QTY" HeaderText="QTY" />
<asp:BoundField DataField="PRICE" HeaderText="PRICE" />
<asp:BoundFie<pre lang="c#">
ld DataField="DISC" HeaderText="DISC" />
<asp:TemplateField HeaderText="AMOUNT">
<itemtemplate>
<asp:Label ID="Label2" runat="server" Font-Bold="true" ForeColor="green" Text='<%# Bind("AMOUNT") %>'>
<asp:TemplateField HeaderText="CANCEL">
<itemtemplate>
<asp:ImageButton ID="ImageButton2" OnClick="ImageButton2_Click" runat="server" ImageAlign="AbsMiddle"
ImageUrl="~/Scripts/1461239982_close_blue.png" />
<HeaderStyle BackColor="#01A5FF" Font-Bold="False" ForeColor="White" BorderStyle="Solid"
BorderWidth="1px" Height="30px" HorizontalAlign="Center" VerticalAlign="Middle" />
<pagerstyle backcolor="#99CCCC" forecolor="#003399" horizontalalign="Left">
<rowstyle forecolor="#003399" borderstyle="Solid" borderwidth="1px" horizontalalign="Center">
VerticalAlign="Middle" />
<SelectedRowStyle BackColor="#009999" Font-Bold="True" ForeColor="#CCFF99" />
<sortedascendingcellstyle backcolor="#EDF6F6">
<sortedascendingheaderstyle backcolor="#0D4AC4">
<sorteddescendingcellstyle backcolor="#D6DFDF">
<sorteddescendingheaderstyle backcolor="#002876">
and code behind is
private void AddNewRecordRowToGrid()
{
// check view state is not null
if (ViewState["invoice"] != null)
{
//get datatable from view state
DataTable dtCurrentTable = (DataTable)ViewState["invoice"];
DataRow drCurrentRow = null;
if (dtCurrentTable.Rows.Count > 0)
{
for (int i = 1; i <= dtCurrentTable.Rows.Count; i++)
{
int UserQty = Convert.ToInt32(TextBox2.Text);
int UserPrice = Convert.ToInt32(TextBox3.Text);
int UserDisc;
if (TextBox4.Text == null||TextBox4.Text=="")
{
UserDisc = 0;
}
else
{
UserDisc = Convert.ToInt32(TextBox4.Text);
}
int linetotal = (UserQty * UserPrice) - (UserQty * UserDisc);
//add each row into data table
drCurrentRow = dtCurrentTable.NewRow();
drCurrentRow["ITEM NAME"] = TextBox1.Text;
drCurrentRow["QTY"] = TextBox2.Text;
drCurrentRow["PRICE"] =TextBox3.Text;
drCurrentRow["DISC"] =UserDisc;
drCurrentRow["AMOUNT"] = linetotal;
}
//Remove initial blank row
if (dtCurrentTable.Rows[0][0].ToString() == "")
{
dtCurrentTable.Rows[0].Delete();
dtCurrentTable.AcceptChanges();
}
//add created Rows into dataTable
dtCurrentTable.Rows.Add(drCurrentRow);
//Save Data table into view state after creating each row
ViewState["invoice"] = dtCurrentTable;
//Bind Gridview with latest Row
GridView1.DataSource = dtCurrentTable;
GridView1.DataBind();
Con.Close();
TextBox1.Text = "";
TextBox2.Text = "";
TextBox3.Text = "";
TextBox4.Text = "";
TextBox1.Focus();
}
<pre lang="c#">for (int i = 0; i < GridView1.Rows.Count; i++)
{
sum += int.Parse(GridView1.Rows[i].Cells[7].Text);
GrandTotal.Text = sum.ToString();
}
}
}
What I have tried:
for (int i = 0; i < GridView1.Rows.Count; i++)
{
sum += int.Parse(GridView1.Rows[i].Cells[7].Text);
GrandTotal.Text = sum.ToString();
}
Try this
foreach (DataRow row in dtCurrentTable.Rows) { int UserQty =0, UserPrice=0, UserDisc=0; int.TryParse(row["UserQty_ColumnName"] + "", out UserQty); int.TryParse(row["UserPrice_ColumnName"] + "", out UserPrice); int.TryParse(row["UserDisc_ColumnName"] + "", out UserDisc); int linetotal = (UserQty * UserPrice) - (UserQty * UserDisc); sum += linetotal; } GrandTotal.Text = sum.ToString(); GridView1.DataSource = dtCurrentTable; GridView1.DataBind();
这篇关于如何找到viewstate中动态gridview列的总和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!