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 相关
基础教程推荐
- rabbitmq 的 REST API 2022-01-01
- 将 Office 安装到 Windows 容器 (servercore:ltsc2019) 失败,错误代码为 17002 2022-01-01
- c# Math.Sqrt 实现 2022-01-01
- 将 XML 转换为通用列表 2022-01-01
- MS Visual Studio .NET 的替代品 2022-01-01
- 如何在 IDE 中获取 Xamarin Studio C# 输出? 2022-01-01
- 为什么Flurl.Http DownloadFileAsync/Http客户端GetAsync需要 2022-09-30
- SSE 浮点算术是否可重现? 2022-01-01
- 如何激活MC67中的红灯 2022-01-01
- 有没有办法忽略 2GB 文件上传的 maxRequestLength 限制? 2022-01-01