我的要求是我希望数据库中的所有数据都绑定到 网格视图而不更新网格视图数据源....
这是我的代码: -
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 :(得分: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();
}
答案 1 :(得分:0)
尝试:
SqlCommand cmd =new SqlCommand("Select * from SiteMaster",con);
我已经更改了sql查询