ASP.net 应用程序未在使用 WCF 服务的网格视图控件中显示总计

Posted

技术标签:

【中文标题】ASP.net 应用程序未在使用 WCF 服务的网格视图控件中显示总计【英文标题】:ASP.net Applications not displaying the grand total into grid view control with WCF service 【发布时间】:2021-07-07 19:46:27 【问题描述】:

我正在尝试使用网格视图控件将总计显示到 asp.net Web 应用程序中。我正在使用 WCF 服务通过 ID(帐号)检索数据表单数据库。我使用 Row Data Bound 事件来计算总数,但问题是不显示总计。

这是 WCF 代码。

public DataSet DepositDetails(Current_Account_Deposit_Details current_Account_Deposit_Details)
        
            SqlConnection con = new SqlConnection(ConnectionString);
            con.Open();
            SqlCommand cmd = new SqlCommand("select * from Current_Account_Deposit where Account_Number=@Account_Number", con);
            cmd.Parameters.AddWithValue("@Account_Number", current_Account_Deposit_Details.Account_Number);
            SqlDataAdapter sda = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            sda.Fill(ds);
            cmd.ExecuteNonQuery();
            con.Close();
            return ds;
          

这是 Web 应用程序的代码。

    protected void Button1_Click(object sender, EventArgs e)
    
        MyService.HalifaxCurrentAccountServiceClient my = new MyService.HalifaxCurrentAccountServiceClient("NetTcpBinding_IHalifaxCurrentAccountService");

        MyService.Current_Account_Deposit_Details cd = new MyService.Current_Account_Deposit_Details();
        cd.Account_Number = TextBox1.Text;

        DataSet ds = new DataSet();
        ds = my.DepositDetails(cd);
        GridView1.DataSource = ds;
        GridView1.DataBind();



    
    int totalDeposit = 0;
  

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    

        // Loop thru each data row and compute total unit price and quantity sold
        if (e.Row.RowType == DataControlRowType.DataRow)
        
            totalDeposit += Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "Amount"));
           
        
        // Display totals in the gridview footer
        else if (e.Row.RowType == DataControlRowType.Footer)
        
            e.Row.Cells[1].Text = "Grand Total";
            e.Row.Cells[1].Font.Bold = true;

           

            e.Row.Cells[3].Text = totalDeposit.ToString();
            e.Row.Cells[3].Font.Bold = true;

           
        
    

这是结果的屏幕截图。

【问题讨论】:

【参考方案1】:

要获取datagridview中的行数吗?

如果有,试试dataGridView1.Rows.Count获取总行数,然后用控件显示出来。

这是参考:DataGridView.RowCount Property

【讨论】:

这是窗体应用程序。如您所见,这个 asp.net Web 应用程序 这是 asp.net Web 应用程序网格视图:docs.microsoft.com/en-us/dotnet/api/… 感谢您的建议,但您提供的链接我已经看到了。对我来说,数据源是由 wcf 服务而不是 sql 数据源提供的。这是我面临的困难 我尝试使用行绑定事件但没有成功 可以尝试在wcf服务中使用sqlcommand.ExecuteScalar()获取满足条件的记录数,然后使用wcf服务获取该值。

以上是关于ASP.net 应用程序未在使用 WCF 服务的网格视图控件中显示总计的主要内容,如果未能解决你的问题,请参考以下文章

Asp.Net:将数据从 WCF 服务广播到 Asp.Net 客户端的最佳方式

带有 asp.net mvc 应用程序的 WCF 服务

使用 JSON 实现带有 WCF 服务 (wshttpBinding) 的 c# asp.net 应用程序

从 ASP.NET 应用程序验证 WCF 服务

在 WCF 服务中使用 ASP.NET 成员资格提供程序身份验证

asp.net web 方法和 wcf 服务有啥区别?