【问题标题】:How to bind gridview to a wcf service application?如何将 gridview 绑定到 wcf 服务应用程序?
【发布时间】:2013-05-16 19:22:31
【问题描述】:

我想根据从 wcf 服务中检索到的数据绑定我的 gridview。但它只显示 gridview 中的最后一行数据,而不是全部显示。

这是我的 WCF:

try
{
  DSCustomer dscat = new DSCustomer();
   //input is EmpUserID
   cmd.Parameters.AddWithValue("@myuser", id);
   cmd.CommandText = "mystoredproc";
   List<DSCustomer> lst = new List<DSCustomer>();
   SqlDataReader dr = cmd.ExecuteReader();

   while (dr.Read())
   {
      dscat.MyEmpID = Convert.ToInt32(dr["Emp"]);
      dscat.MyEmpName = dr["EmpName"].ToString();
      dscat.MyUnitName = dr["UnitName"].ToString();
      dscat.MyUnitNumber = Convert.ToInt32(dr["Unit"]);
      dscat.MyRole = dr["Role"].ToString();
      dscat.MySurveyStatus = dr["SurveyStatus"].ToString();

      //Add all the returns in to the list from back-end
      lst.Add(dscat);
   }

   //returns to the list
   return lst;
}

这是 DScustomer

public class DSCustomer
    {
        //Created properties based on the count of the data that we want to retrieve
        public int MyEmpID { get; set; }
        public string MyEmpName { get; set; }
        public string MyUnitName { get; set; }
        public int MyUnitNumber { get; set; }
        public string MyRole { get; set; }
        public string MySurveyStatus { get; set; }

    }

还有我的 default.aspx:

protected void Button1_Click(object sender, EventArgs e)
{
   MyServiceClient client = new MyServiceClient();
   Customer cust = new Customer();

   cust = client.getCategori(tbEmpID.Text);

   var list = new List<Customer> { cust };
   GridView1.DataSource=list;
   GridView1.DataBind();
}

【问题讨论】:

  • 愚蠢的问题。这是服务的真实代码吗?我在 while 循环中看不到任何地方实例化 dscat 的新实例。所以这些值每次都会被覆盖。
  • 请不要这样。只是分享你的答案,我没有分享这个问题来听到你的声音。
  • @Abdullah WCF 方法签名是什么,client.getCategori 返回类型是什么?
  • 我并没有粗鲁,有时人们发布的内容接近实际代码,但实际上并不是真正的代码。 “愚蠢的问题”指的是我的问题,而不是你的问题。

标签: c# asp.net wcf


【解决方案1】:

问题是我认为你调用了不同的服务

Customer cust = new Customer();
cust = client.getCategori(tbEmpID.Text); // this method only return one customer 
var list = new List<Customer> { cust };
GridView1.DataSource=list;
GridView1.DataBind();

在您给定的服务中,您将返回 List,因此您可以直接将其绑定到 DataGrid

GridView1.DataSource=client.getCategori(tbEmpID.Text).AsEnumerable() ;
GridView1.DataBind();

还有一件事,在 while 循环中创建新的 DSCustomer 并将其添加到最后的列表中

   while (dr.Read())
   {
      DSCustomer cust = new DSCustomer();
      cust.MyEmpID = Convert.ToInt32(dr["Emp"]);
      cust.MyEmpName = dr["EmpName"].ToString();
      cust.MyUnitName = dr["UnitName"].ToString();
      cust.MyUnitNumber = Convert.ToInt32(dr["Unit"]);
      cust.MyRole = dr["Role"].ToString();
      cust.MySurveyStatus = dr["SurveyStatus"].ToString();
      lst.Add(cust);
   }

【讨论】:

  • 它基于empid。当我在后端运行它时,显示两条记录。
  • 我尝试过这种方式,但显示数据源是无效类型。它必须是 IListSource、IEnumerable 或 IDataSource。错误
  • @Abdullah 尝试在最后使用.AsEnumerable()
  • 它不工作。 :( , 尝试绑定 Dropdowsnlist 时也会发生同样的情况。关键点是什么?,
  • 你在同一个解决方案中有 WCF 和 ASP.Net 项目吗?同时启动两者并将刹车点投入使用。单击按钮并从 ASP.net 调用服务,看看从服务端返回 WCF 服务的值是什么,并从 asp.net 端检查。
【解决方案2】:

声明 dscat 变量的行:

DSCustomer dscat = new DSCustomer();

应该移到 while 循环内。虽然您可能会向 lst 添加 N 个元素,但 lst 中的每个 DSCustomer 项将具有与添加到 lst 列表中的最后一项相同的值。

还要注意您对 WCF 服务的调用:

Customer cust = new Customer();
cust = client.getCategori(tbEmpID.Text);

表明您只会得到 1 个客户对象(不是很多),然后您从该对象创建一个包含 1 个项目的列表:

var list = new List<Customer> { cust }; // list only has 1 item.

因此,您为 WCF 服务显示的代码似乎与您在客户端上调用的方法不同。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-28
    • 2020-09-26
    • 1970-01-01
    • 2013-09-30
    • 1970-01-01
    相关资源
    最近更新 更多