2014-02-12 47 views
0

我有一個需要顯示許多網格視圖的項目。網格視圖的數量取決於目錄所具有的表的數量。處理以編程方式創建的網格視圖的排序事件

例如:

指南A = 8個表

指南B = 7個表

如果用戶點擊目錄A,應該創建8個GridView的。 我已經完成了以編程方式創建這個gridviews,我現在的問題是我該如何處理每個以編程方式創建的gridviews排序事件。

這是我如何創建我的GridView的:

foreach (XMLClasses.table dirTab in dir.table.ToList()) 
{ 
    if (dirTab.id == child.tabid) 
    { 
     List<XMLClasses.column> columns = new List<XMLClasses.column>(); 
     columns = dirTab.column; 
     string[] rows = new string[columns.Count]; 
     int x = 0; 
     foreach (XMLClasses.column col in columns.ToList()) 
     { 
      dtContent.Columns.Add(col.title); 
      rows[x] = "b"; 
      x = x + 1; 
     } 
     dtContent.Rows.Add(rows); 
     GridView grdTables = new GridView(); 
     grdTables.AllowSorting = true; 
     grdTables.DataSource = dtContent; 
     grdTables.DataBind(); 
     grdTables.Width = Unit.Percentage(100); 
     grdTables.Sorting+=new GridViewSortEventHandler(grdTables_Sorting); 

     pnlDirectory.Controls.Add(grdTables); 

     Literal lt = new Literal(); 
     lt.Text = "<br/>"; 
     pnlDirectory.Controls.Add(lt); 

    } 
} 

IM股票與此有關。我不知道下一步該怎麼做..

有人有什麼想法嗎? 任何幫助,將不勝感激!

謝謝!

+0

你能發佈一些代碼,請 – Rohan

+0

編輯它。 @Rohan – danielle

回答

0

如果您能夠以編程方式創建gridviews併爲其設置名稱,則可以處理排序。

void SortButton_Click(Object sender, EventArgs e) { 

String expression = ""; 
SortDirection direction; 

// Create the sort expression from the values selected 
// by the user from the DropDownList controls. Multiple 
// columns can be sorted by creating a sort expression 
// that contains a comma-separated list of field names. 
expression = SortList1.SelectedValue + "," + SortList2.SelectedValue; 

// Determine the sort direction. The sort direction 
// applies only to the second column sorted. 
switch (DirectionList.SelectedValue) 
{ 
    case "Ascending": 
    direction = SortDirection.Ascending; 
    break; 
    case "Descending": 
    direction = SortDirection.Descending; 
    break; 
    default: 
    direction = SortDirection.Ascending; 
    break; 
} 

// Use the Sort method to programmatically sort the GridView 
// control using the sort expression and direction. 
CustomersGridView.Sort(expression, direction); 

}

這裏會發生什麼事是兩個屬性被創建的;表達和方向。

通過使用sortDirection對象來設置排序(降序或升序)和字符串「表達式」,可用於將某些柱面作爲目標進行排序並將其用作.Sort方法的參數,您所需的效果可以使用。

編輯

忘記添加代碼的來源。

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.sort(v=vs.110).aspx

+0

我需要在用戶點擊列時觸發排序事件 – danielle

+0

您可以在事件處理程序中使用此代碼並獲取列屬性以使用正確的值填充「表達式」,因此Sort方法可以爲您完成。 http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.sorting(v=vs.110).aspx – Saidin

相關問題