如何绑定的GridView到WCF服务应用程序? [英] How to bind gridview to a wcf service application?

查看:84
本文介绍了如何绑定的GridView到WCF服务应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想基于从WCF服务检索数据的GridView我绑定。但它显示了在GridView中只有最后一排数据,而不是展示这一切。

下面是我的WCF:

 尝试
{
  DSCustomer dscat =新DSCustomer();
   //输入EmpUserID
   cmd.Parameters.AddWithValue(@为myuser,身份证);
   cmd.CommandText =mystoredproc;
   清单< D​​SCustomer> LST =新的List< D​​SCustomer>();
   SqlDataReader的博士= cmd.ExecuteReader();   而(dr.Read())
   {
      dscat.MyEmpID = Convert.ToInt32(DR [的Emp]);
      dscat.MyEmpName =博士[EmpName]的ToString()。
      dscat.MyUnitName =博士[单位名]的ToString()。
      dscat.MyUnitNumber = Convert.ToInt32(DR [单位]);
      dscat.MyRole =博士[角色]的ToString()。
      dscat.MySurveyStatus =博士[SurveyStatus]的ToString()。      //在将所有的投资收益,从后端名单
      lst.Add(dscat);
   }   //返回到列表
   返回善堂;
}

这是DScustomer

 公共类DSCustomer
    {
        基于我们要检索的数据计数//创建的属性
        公众诠释MyEmpID {搞定;组; }
        公共字符串MyEmpName {搞定;组; }
        公共字符串MyUnitName {搞定;组; }
        公众诠释MyUnitNumber {搞定;组; }
        公共字符串MyRole {搞定;组; }
        公共字符串MySurveyStatus {搞定;组; }    }

和我的Default.aspx:

 保护无效的button1_Click(对象发件人,EventArgs的发送)
{
   MyServiceClient客户端=新MyServiceClient();
   客户卡斯特=新客户();   卡斯特= client.getCategori(tbEmpID.Text);   VAR名单=新名单,LT;客户> {}卡斯特;
   GridView1.DataSource =清单;
   GridView1.DataBind();
}


解决方案

在这里你声明dscat变量的行:

  DSCustomer dscat =新DSCustomer();

如果在while循环的内部移动。虽然你可能会添加元素LST N多,在各善堂项目DSCustomer都会有相同的值最后一个项目添加到列表善堂

另外请注意,您的调用WCF服务:

 客户卡斯特=新客户();
卡斯特= client.getCategori(tbEmpID.Text);

显示,你将只能得到1顾客对象返回(不是很多),然后从该对象中创建项目1项的列表:

  VAR名单=新名单,LT;客户> {}卡斯特; //名单只有1项。

如此看来,在code你显示了WCF服务是不是你所呼叫的客户机上的同样的方法。

I want to bind my gridview based on the retrieved data from wcf service .But it shows only last row data in gridview instead of showing them all.

Here is my 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;
}

this is 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; }

    }

And my 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();
}

解决方案

The line where you declare the dscat variable:

DSCustomer dscat = new DSCustomer();

Should be moved inside of the while loop. While you might be adding N number of elements to lst, each DSCustomer item in lst will have the same values as last item added to the lst list.

Also note that your call to the WCF service:

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

Shows that you will only get 1 customer object back (not many), and then you create a list of 1 item from that object:

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

So it seems that the code you show for the WCF service is not the same method that you are calling on the client.

这篇关于如何绑定的GridView到WCF服务应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆