我正在开发一个学校ERP项目作为ASP.NET Web应用程序,我们在其中有一个gridview。在我的页面上,我想计算gridview中行的单元格值的总和,但我使用的代码返回特定列总数的两到三倍。关于我们如何计算特定列的正确总数的任何建议?
注意:这里我们有一个从数据库填充的复选框列表,chekboxlist的项目是gridview的列,当我们检查chekboxlist中的项目列动态添加gridview时,当我们检查cheboxlist中的第二个项目然后第二个列添加了int gridview如图所示
我想要的结果(输出)
我的c#代码。
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
namespace egurukul
{
public partial class feerecipts : System.Web.UI.Page
{
decimal val;
string _connStr = ConfigurationManager.ConnectionStrings["con1"].ConnectionString;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
bindgvfeehead();
BindTableColumns();
}
}
private void BindTableColumns()
{
DataTable table = new DataTable();
using (SqlConnection conn = new SqlConnection(_connStr))
{
SqlCommand cmd = new SqlCommand("Readduedat", conn);
cmd.CommandType = CommandType.StoredProcedure;
// get the adapter object and attach the command object to it
SqlDataAdapter ad = new SqlDataAdapter(cmd);
// fire Fill method to fetch the data and fill into DataTable
ad.Fill(table);
}
chbxlstduedate.DataSource = table;
chbxlstduedate.DataTextField = "duemonth";
chbxlstduedate.DataValueField = "duedateid";
chbxlstduedate.DataBind();
}
public void bindgvfeehead()
{
SqlConnection
con = new SqlConnection();
con.ConnectionString = ConfigurationManager.ConnectionStrings["con1"].ConnectionString;
con.Open();
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "readfeehead";
cmd.CommandType = CommandType.StoredProcedure;
cmd.Connection = con;
cmd.Parameters.AddWithValue("@classname", Convert.ToString(ddlclassname.SelectedItem));
cmd.Parameters.AddWithValue("@feecatename", Convert.ToString(ddlstcategory.SelectedItem));
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable table = new DataTable();
da.Fill(table);
gvfeehead.DataSource = table;
gvfeehead.DataBind();
con.Close();
}
private void GetData()
{
DataTable table = new DataTable();
// get the connection
SqlConnection conn = new SqlConnection(_connStr);
conn.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandText = "readfeedetails";
cmd.CommandType = CommandType.StoredProcedure;
// instantiate the command object to fire
cmd.Parameters.AddWithValue("@classname", Convert.ToString(ddlclassname.SelectedItem));
cmd.Parameters.AddWithValue("@feecatename", Convert.ToString(ddlstcategory.SelectedItem));
SqlDataAdapter ad = new SqlDataAdapter(cmd);
// fire Fill method to fetch the data and fill into DataTable
ad.Fill(table);
conn.Close();
// specify the data source for the GridView
GridView1.DataSource = table;
// bind the data now
GridView1.DataBind();
}
protected void chbxlstduedate_SelectedIndexChanged(object sender, EventArgs e)
{
foreach (ListItem item in chbxlstduedate.Items)
{
if (item.Selected)
{
BoundField b = new BoundField();
b.DataField = item.Text;
b.HeaderText = item.Text;
GridView1.Columns.Add(b);
}
}
TemplateField tfield = new TemplateField();
tfield.HeaderText = "Total";
GridView1.Columns.Add(tfield);
this.GetData();
bindgvfeehead();
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
for (int i = 0; i < GridView1.Rows.Count; i++)
{
int j;
double Total = 0.00;
for (j = 0; j < GridView1.Columns.Count; j++)
{
Total += GridView1.Rows[i].Cells[j].Text != " " ? double.Parse(GridView1.Rows[i].Cells[j].Text) : 0.000;
}
int k = Convert.ToInt32(GridView1.Columns.Count);
GridView1.Rows[i].Cells[k - 1].Text = Total.ToString("#0.000");
}
}
}
}
我的aspx代码
<div style="float:left">
<asp:GridView ID="gvfeehead" runat="server" AutoGenerateColumns="False"
BackColor="White" BorderColor="#336666" BorderStyle="Double"
Font-Size="11px" BorderWidth="3px"
CellPadding="4" GridLines="both" ShowFooter="True">
<RowStyle ForeColor="#333333" BackColor="White" Height="20px" />
<Columns>
<asp:TemplateField>
<HeaderTemplate>Sr.No </HeaderTemplate>
<ItemTemplate> <%#Container.DataItemIndex+1 %></ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate>Fee Name </HeaderTemplate>
<ItemTemplate>
<asp:Label ID="lblfeename" runat="server" Text='<%#Eval("feename") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate> <asp:Label ID="lblfeename" CssClass="footer_text" runat="server" Text="Total"></asp:Label></FooterTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="White" ForeColor="#333333" />
<PagerStyle BackColor="#336666" ForeColor="White" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#339966" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#336666" Font-Bold="True" ForeColor="White" />
</asp:GridView>
</div>
<div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
BackColor="White" BorderColor="#336666" BorderStyle="Double"
Font-Size="11px" BorderWidth="3px"
CellPadding="4" GridLines="both"
ShowFooter="True" EnableViewState="false"
onrowdatabound="GridView1_RowDataBound">
<RowStyle ForeColor="#333333" BackColor="White" Height="21px" />
<Columns>
</Columns>
<FooterStyle BackColor="White" ForeColor="#333333" Font-Bold="true" />
<PagerStyle BackColor="#336666" ForeColor="White" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#339966" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#336666" Font-Bold="True" ForeColor="White" />
</asp:GridView>
</div>
<div class="duedateheading">
<asp:Label ID="lbl_formonth" runat="server" Font- Bold="True" Text="For Month"></asp:Label>
</div>
<asp:CheckBoxList ID="chbxlstduedate" runat="server" AutoPostBack="true" onselectedindexchanged="chbxlstduedate_SelectedIndexChanged">
</asp:CheckBoxList>
</div>
答案 0 :(得分:0)
第一行的值返回正确的值,以便在null检查时可能会发生问题尝试使用Trim()
Total += GridView1.Rows[i].Cells[j].Text.Trim() != " " ? double.Parse(GridView1.Rows[i].Cells[j].Text.Trim()) : 0.000;
你还需要检查gridview的行索引是否正确地进入循环