-1

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

DSカスタマーです

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

2 に答える 2

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

指定されたサービスでは List を返すため、DataGrid に直接バインドできます

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

もう1つ、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);
   }
于 2013-05-16T19:32:20.297 に答える