2012-03-02 65 views
0

我有一個綁定到我的RadGrid的對象數據源。但是,不要向下擴大,我希望它向右擴展。 。
對於離,一般的格柵頂部有頁眉和下面的數據吧:Telerik RadGrid爲每個對象添加數據列而不是行

Column A Column B Column C 
Value A 1   9   
Value B 3   7 
Value C 15   17   

相反,我想在左邊和價值觀右側的列標題。

Column A Value A Value B Value C 
Column B 1   9   15 
Column C 3   7   17 

如何實現它?

回答

3

的.aspx

<telerik:RadGrid ID="RadGrid1" Width="95%" runat="server" OnItemCreated="RadGrid1_ItemCreated" 
     OnNeedDataSource="RadGrid1_NeedDataSource1"> 
     <PagerStyle Mode="NextPrevAndNumeric" /> 
    </telerik:RadGrid> 

.aspx.cs

public DataTable GetDataTable(string query) 
    { 
     String ConnString = ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString; 
     SqlConnection conn = new SqlConnection(ConnString); 
     SqlDataAdapter adapter = new SqlDataAdapter(); 
     adapter.SelectCommand = new SqlCommand(query, conn); 

     DataTable myDataTable = new DataTable(); 

     conn.Open(); 
     try 
     { 
      adapter.Fill(myDataTable); 
     } 
     finally 
     { 
      conn.Close(); 
     } 

     return myDataTable; 
    } 


    public DataTable PivotTable(DataTable source) 
    { 
     DataTable dest = new DataTable("Pivoted" + source.TableName); 

     dest.Columns.Add(" "); 

     foreach (DataRow r in source.Rows) 
      dest.Columns.Add(r[0].ToString()); 

     for (int i = 0; i < source.Columns.Count - 1; i++) 
     { 
      dest.Rows.Add(dest.NewRow()); 
     } 

     for (int r = 0; r < dest.Rows.Count; r++) 
     { 
      for (int c = 0; c < dest.Columns.Count; c++) 
      { 
       if (c == 0) 
        dest.Rows[r][0] = source.Columns[r + 1].ColumnName; 
       else 
        dest.Rows[r][c] = source.Rows[c - 1][r + 1]; 
      } 
     } 
     dest.AcceptChanges(); 
     return dest; 
    } 

    protected void RadGrid1_NeedDataSource1(object source, GridNeedDataSourceEventArgs e) 
    { 
     RadGrid1.DataSource = PivotTable(GetDataTable("SELECT TOP 5 CustomerID, CompanyName, ContactName, ContactTitle, Address, PostalCode, City, Country FROM Customers")); 
    } 
    protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e) 
    { 
     if (e.Item is GridDataItem) 
     { 
      (e.Item as GridDataItem)[(RadGrid1.MasterTableView.AutoGeneratedColumns[0] as GridBoundColumn).UniqueName].Font.Bold = true; 
      (e.Item as GridDataItem)[(RadGrid1.MasterTableView.AutoGeneratedColumns[0] as GridBoundColumn).UniqueName].BackColor = System.Drawing.SystemColors.Control; 
      (e.Item as GridDataItem)[(RadGrid1.MasterTableView.AutoGeneratedColumns[0] as GridBoundColumn).UniqueName].BorderColor = System.Drawing.Color.White; 
     } 
    } 
+0

大答案Jayesh ...感謝一大堆。 – 2012-03-04 20:55:57

+0

非常感謝,這節省了我的時間.. – Srinivas 2015-06-05 14:07:08

相關問題