2013-05-16 34 views
-1

我想根据从wcf服务检索到的数据绑定我的gridview。但它只显示gridview中的最后一行数据,而不是全部显示它们。如何将gridview绑定到wcf服务应用程序?

这里是我的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(); 
} 
+0

愚蠢的问题。这是服务的真实代码吗?我在while循环中没有看到任何地方实例化任何dscat的新实例。所以这些值每次都会被覆盖。 – Rich

+1

不要那样。只是分享你的答案,我没有分享这个问题听到你的sl。声。 – Abdullah

+1

@Abdullah什么是WCF方法签名和什么是'client.getCategori'返回类型? – Damith

回答

0

问题是,我想你调用不同的服务

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(); 

在你给定的服务要返回列表,所以你可以直接把它绑定到数据网格

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); 
    } 
+0

IT基于empid。当我在后台运行它时,显示两条记录。 – Abdullah

+0

我试过这种方式,但显示我数据源是一个无效的类型。它必须是IListSource,IEnumerable或IDataSource。错误 – Abdullah

+0

@Abdullah尝试用'.AsEnumerable()'在末尾 – Damith

0
列出

,你声明DS阳变量行:

DSCustomer dscat = new DSCustomer(); 

如若WH的内部移动ile循环。虽然您可能会添加N个元素到第1个元素,但第1个元素中的每个DSCustomer元素将与添加到第1个元素列表中的最后一个元素具有相同的值。

另外请注意,您的WCF服务电话:

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

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

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

所以看起来您为WCF服务展示的代码与您在客户端上调用的方法不同。

相关问题