GridView 表 1 与表 2 相关

GridView Table 1 related to Table 2(GridView 表 1 与表 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 相关

基础教程推荐