的GridView表1与表2 [英] GridView Table 1 related to Table 2
问题描述
对不起,我知道标题实在是混乱,但我无法弄清楚到底该怎么放了下来。
Sorry I know Title is really confusing but I couldn't figure out what exactly to put down.
基本上,我创建了一个网格视图用于查询数据库,并显示数据。它完美地工作,没有抱怨,但是我有什么现在的问题是,
Basically I created a Grid View which queries database and displays data. It works perfectly, no complain, however what I have right now is,
但我要的是,
问:我不知道我怎么能做到这一点,可以有人只是点我出去在朝着正确的方向吗?
Question: I am not sure how can I do this, can someone just point me out in right direction please ?
我想我会去使用嵌套GridView的。
I think I will going to use nested gridviews.
推荐答案
尝试改变你的 SELECT
查询像下面...这将您获得的预期结果 ...
Try to change your SELECT
Query like below... It will you to get the Expected Result...
SQL小提琴:的 http://www.sqlfiddle.com/#!3/00b5f/15
我有一个名为表
为水果
SELECT CrateTitle,CrateDescription,CrateID,
stuff(
(
SELECT ','+ [FruitTitle] FROM fruits WHERE CrateID = t.CrateID FOR XML path('')
),1,1,'') Types_of_Fruits_in_Crate
FROM (SELECT DISTINCT CrateTitle,CrateDescription,CrateID FROM fruits )t
或
创建一个PROC
*把这个查询在PROC *
*呼叫PROC *
*的结果集的GridView *分配
您可以将自己存储过程的结果通过使用下面的代码设置为GridView的:
You can Assign he Stored Proc Result set to GridView by using the Below Code :
DataTable dt = new DataTable();
SqlConnection connection = new SqlConnection("Your Connection String");
try
{
connection.Open();
string spName = "YOURStoredProcudureName";
SqlCommand sqlCmd = new SqlCommand(spName, connection);
SqlDataAdapter sqlDa = new SqlDataAdapter(sqlCmd);
sqlCmd.CommandType = CommandType.StoredProcedure;
sqlDa.Fill(dt);
if (dt.Rows.Count > 0)
{
//display the DataTable to a Data control like GridView for example
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
catch (System.Data.SqlClient.SqlException ex)
{
string msg = "Fetch Error:";
msg += ex.Message;
throw new Exception(msg);
}
finally
{
connection.Close();
}
这篇关于的GridView表1与表2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!