求gridview绑定问题,急急,解决就给分
我有两个表
表A
audioID audioName UserId audioRemark
表B
ID CommTxt(留言) audioID
我想用gridview 通过UserId显示某个用户所有的audio信息
这样显示
audioName audioRemark countcomm(统计留的次数)
这个怎么做,用不用gridview 嵌套
看看,我下面做的,,这个有问题,,谢谢指点!!!
public int commcount; 在html里绑定这个统计字段,
private void MyCreateBind()
{
int uid = Int32.Parse(Session[ "UserId "].ToString());
string str = "select * from UsersAudioView where UserId= " + uid;
SqlConnection myCon = new SqlConnection(ConfigurationManager.ConnectionStrings[ "mysql "].ConnectionString);
myCon.Open();
SqlDataAdapter sda = new SqlDataAdapter(str, myCon);
DataSet ds = new DataSet();
sda.Fill(ds, "UsersAudioView ");
AudioInfoGV.DataSource = ds.Tables[ "UsersAudioView "];
AudioInfoGV.DataBind();
myCon.Close();
}
protected void audioInfoGV_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
// DataList dali = (DataList)e.Row.FindControl( "DataList1 ");
Label lblvid = (Label)e.Row.FindControl( "lblaudioId ");
string aid= lblvid.Text.ToString();
string str = "select count(*)as aa from 表B where audioId= ' " + aid+ " ' ";
SqlConnection myCon = new SqlConnection(ConfigurationManager.ConnectionStrings[ "mysql "].ConnectionString);
myCon.Open();
SqlCommand myCmd = new SqlCommand(str, myCon);
// SqlDataAdapter sda = new SqlDataAdapter(str, myCon);
// DataSet ds = new DataSet();
// sda.Fill(ds);
SqlDataReader dr = myCmd.ExecuteReader();
if (dr.Read())
{
commcount = Int32.Parse(dr[ "aa "].ToString());
}
else
{
commcount = 0;
}
// dr.Close();
// dali.DataSource = ds;
// dali.DataBind();
dr.close();
}
}
[解决办法]
我的感,你的程序在audioInfoGV_RowDataBound里面行的很慢,我得你不需要去按每行都是查B表一次,速度太慢了。建:我不知道你要示的字段,所以分步理。
DataSet ds = new DataSet();
string strA = "select * from A where UserId= " + uid;
.......
sda.Fill(ds , "A ");
string strB = "select A.audioId,count(B.*) as aa from A inner join B on A.audioId = b.audioId where A.UserId = " + uid + " Group by A.audioId "
sda.fill(ds , "B ");
,A表B表就是一一的系,A表的留言就在B表的aa字段里面,字段audioId。(如果A表的某audioId在B表中有,那它的留言就是0.)