2017-05-29 111 views
2

我的要求是,我希望所有從數據庫中的數據應該綁定到 網格視圖不更新網格視圖數據源....GridView的數據源更新

這裏是我的代碼: -

public void BindAll(GridView grd) 
     { 
      List<int> id = new List<int>(); 
      SqlCommand cmd =new SqlCommand("Select SiteId from SiteMaster",con); 

      con.Open(); 
      SqlDataReader dr =cmd.ExecuteReader(); 
      while (dr.Read()) 
      { 
       id.Add(Convert.ToInt16(dr["SiteId"])); 

      } 
      dr.Close(); 
      foreach (int k in id) 
      { 
      List<Errorlog> lst = new List<Errorlog>(); 
      DynamicParameters param = new DynamicParameters(); 
      param.Add("SiteId",k, DbType.Int16); 
      lst = con.Query<Errorlog>("Usp_Temp", param, null, true, 200, CommandType.StoredProcedure).ToList(); 
      if (lst.Count != 0) 
      { 

       grd.DataSource = lst; //here it display only those record which are last updated.I am binding data from multiple table .it only display last table data.I want all the data from all the table should be display. 
       grd.DataBind(); 
      } 


      } 

     } 
+0

我不想更新lst只打一次電話:清單 lst =新清單();每次刪除舊數據時都會創建一個新列表。另外當更新設置數據源爲空時:grd.DataSource = null; grd.DataSource = lst; – jdweng

回答

2
List<Errorlog> lst = new List<Errorlog>(); // create list 
// loop and add items to above list as below 
foreach (int k in id) 
{ 
    DynamicParameters param = new DynamicParameters(); 
    param.Add("SiteId",k, DbType.Int16); 
    List<Errorlog> temp= con.Query<Errorlog>("Usp_Temp", param, null, true, 200, CommandType.StoredProcedure).ToList(); 
    //add to main list 
    lst .AddRange(temp); 

} 
//finally show all the data 
if (lst.Count != 0) 
{ 
    grd.DataSource = lst; 
    grd.DataBind(); 
} 
+0

感謝您的解決方案 – Hinal

0

嘗試:

SqlCommand cmd =new SqlCommand("Select * from SiteMaster",con); 

我已經改變了SQL查詢