GridView Table 1 related to Table 2(GridView 表 1 与表 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 ?
我想我会使用嵌套的网格视图.
I think I will going to use nested gridviews.
推荐答案
Try to change your SELECT
Query like below... It will you get the Expected Result...
Try to change your SELECT
Query like below... It will you to get the Expected Result...
SQL 小提琴: http://www.sqlfiddle.com/#!3/00b5f/15
我将Table
命名为Fruits
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*
*将该结果集分配给 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 相关的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:GridView 表 1 与表 2 相关


- 良好实践:如何重用 .csproj 和 .sln 文件来为 CI 创建 2022-01-01
- C#MongoDB使用Builders查找派生对象 2022-09-04
- 如何用自己压缩一个 IEnumerable 2022-01-01
- C# 中多线程网络服务器的模式 2022-01-01
- 在哪里可以找到使用中的C#/XML文档注释的好例子? 2022-01-01
- 带有服务/守护程序应用程序的 Microsoft Graph CSharp SDK 和 OneDrive for Business - 配额方面返回 null 2022-01-01
- 输入按键事件处理程序 2022-01-01
- MoreLinq maxBy vs LINQ max + where 2022-01-01
- Web Api 中的 Swagger .netcore 3.1,使用 swagger UI 设置日期时间格式 2022-01-01
- WebMatrix WebSecurity PasswordSalt 2022-01-01