2013-04-25 58 views
2

對不起,我知道標題真的很混亂,但我無法弄清楚究竟要放下什麼。GridView與表2相關的表1

基本上我創建了一個查詢數據庫和顯示數據的網格視圖。它完美的作品,沒有抱怨,但是我有什麼現在的問題是,

enter image description here

,但我要的是,

enter image description here

問:我不知道我怎麼能做到這一點,有人能指出我正確的方向嗎?

我想我會去使用嵌套的gridviews。

+0

如果您正在使用說的EntityDataSource你可以把一個ListView到底coloumn並將其項目綁定到導航屬性上,如果您設置好數據庫和EF模型。 .Net2.0真的嗎? – 2013-04-25 14:57:27

回答

2

試圖改變自己的SELECT查詢像以下...您將得到預期結果 ...

SQL小提琴:http://www.sqlfiddle.com/#!3/00b5f/15

我有一個名爲TableFruits

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 

OR

CREATE a PROC

*Place this Query in that Proc*

*Call that Proc*

*assign that Result set to GridView*

您可以指定使用下面的代碼,他存儲過程的結果集的GridView:

 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(); 
     } 
+0

+1和精彩的,非常感謝,我給它去 – Mathematics 2013-04-25 15:19:47

+0

@ user13814:謝謝,如果它可以幫助你,然後將它標記爲答案,那麼只有它很容易識別到其他人。 – Pandian 2013-04-25 15:25:10

+0

Pandian,如果我想獲得水果重量,那麼這是已經在視圖中的列 – Mathematics 2013-04-25 15:38:21

0

這是更多的SQL(或任何語言你的數據庫引擎使用)問題比交流#問題儘管一個來自C#的解決方案將是(儘管它可能是一些額外的工作)使用HTML文字來繪製表格在運行時

的另一個選項是改變你的SQL,但沒有更多的信息,我不能說,如果你也許可以使用一組通過對changeID或數據透視表

+0

或者可能使用這個http://www.codeproject.com/Articles/189997/Gridview-inside-Gridview-in-asp-net-Csharp – Mathematics 2013-04-25 14:57:13