注册 登录
编程论坛 ASP.NET技术论坛

怎么把多个查询结果绑定在DataGrid控件上?

zfyhome 发布于 2007-03-29 21:44, 696 次点击

我要把好几个表中(这几个表都不关朕)的数据绑定在DataGrid控件中,要怎么做?

6 回复
#2
bygg2007-03-30 00:51
你用一句SQL语句查询出多个表中的数据,再绑定不就行了.
#3
yunj11052007-03-30 09:41
楼上说的对
#4
卡洛2007-03-30 10:39
public void con_news()
{
Class1 con_yj = new Class1();
DataSet ds = new DataSet();
DataTable dt = new DataTable("testif");
//创建表列名
DataColumn dcdate = new DataColumn("date", typeof(System.String));
DataColumn dcpic = new DataColumn("pic", typeof(System.String));
DataColumn dctitle = new DataColumn("title", typeof(System.String));
DataColumn dcid = new DataColumn("id", typeof(System.String));
DataColumn dcauthor = new DataColumn("author", typeof(System.String));
//添加表列名
dt.Columns.Add(dcid);
dt.Columns.Add(dctitle);
dt.Columns.Add(dcpic);
dt.Columns.Add(dcdate);
dt.Columns.Add(dcauthor);
//添加内容
for (int i = 0; i < con_yj.con("select top 5 * from news_info").Tables[0].Rows.Count; i++)
{
DataRow row = dt.NewRow();
if (Convert.ToInt32(con_yj.con("select datediff(dd,new_date,getdate()) from news_info order by new_date desc").Tables[0].Rows[i][0].ToString()) <= 1)
{
row[2] = "img/news.jpg";
}
else
{
row[2] = "img/new.jpg";
}
row[0] = con_yj.con("select * from news_info order by new_date desc").Tables[0].Rows[i]["new_number"].ToString();
if (con_yj.con("select * from news_info order by new_date desc").Tables[0].Rows[i]["new_rubric"].ToString().Length > 25)
{
row[1] = con_yj.con("select * from news_info order by new_date desc").Tables[0].Rows[i]["new_rubric"].ToString().Substring(0, 25) + "......";
}
else { row[1] = con_yj.con("select * from news_info order by new_date desc").Tables[0].Rows[i]["new_rubric"].ToString(); }
row[3] = con_yj.con("select * from news_info order by new_date desc").Tables[0].Rows[i]["new_date"].ToString().Substring(0, 10);
dt.Rows.Add(row);
}
DataView dv = new DataView(dt);
//dv.Sort = "date desc";
dl1.DataSource = dv;
dl1.DataBind();
}

昨天写的新闻显示的一段代码。。。应该可以解决你问的问题了。。

#5
skyland842007-03-30 11:06
楼上的代码 没有时间看了啦!

如果 没有想错!

楼主 是想一个 DATAGRID 就把所以 想要处理的数据显示了!?

那样的话 你可以按照 2楼的处理!

JION 一下 就合并成一个表了!
#6
卡洛2007-03-30 11:09
5楼的兄弟老是搞马后炮吖。。要给点自己的建议吖``
#7
zfyhome2007-03-30 22:29

如果要把几个表的count(*)提取出来,那靠一条查询语句应该实行不了吧?看来只能用四楼的方法了,但这样感觉很麻烦

1