for循环中的存储过程如何显示到一个GridView中c#

[复制链接]
查看11 | 回复1 | 2010-7-22 11:58:20 | 显示全部楼层 |阅读模式
for (int j = 0; j < lstWcq.Items.Count; j++)

{

SqlConnection myConnection = new SqlConnection(ConfigurationSettings.AppSettings["qtcdph"]);

SqlCommand myCommand = new SqlCommand("dian_gy_fx", myConnection);

myCommand.CommandType = CommandType.StoredProcedure;

myConnection.Open();

myCommand.Parameters.Add(new SqlParameter("@sj1", SqlDbType.DateTime));

myCommand.Parameters["@sj1"].Value = TextBox1.Text.Trim();

myCommand.Parameters.Add(new SqlParameter("@sj2", SqlDbType.DateTime));

myCommand.Parameters["@sj2"].Value = TextBox2.Text.Trim();



myCommand.Parameters.Add(new SqlParameter("@id", SqlDbType.NChar));



myCommand.Parameters["@id"].Value = lstWcq.Items[j].ToString();



SqlDataAdapter da = new SqlDataAdapter(myCommand);

DataSet ds = new DataSet();

da.Fill(ds);

GridView1.DataSource = ds;

GridView1.DataBind();



myConnection.Close();

}只能显示最后一个ID的值,如何能把所有的ID查询值都显示到一个GridView中

回复

使用道具 举报

千问 | 2010-7-22 11:58:20 | 显示全部楼层
你这样每次循环都会重新绑定的。肯定是只显示最后一个你需要在外面建一个datatable dtTemp,然后循环里在da.Fill(ds);后面加一句dtTemp.Merge(ds.Table[0]);把结果集合并最后在循环外面GridView1.DataSource = dtTemp;
GridView1.DataBind(); 这样就OK了
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行