2011-05-10 31 views
0

我有一個代碼在網格視圖中執行分頁和排序。在網格視圖中分頁和排序

(bing_grid是用戶定義的函數)

public void bind_grid() 
    { 
     con = new SqlConnection(); 
     con.ConnectionString = "Data Source=STIRAPC105;InitialCatalog=anitha;Integrated Security=True"; 
     con.Open(); 

     SqlDataAdapter sqa = new SqlDataAdapter("select * from employees", con); 
     DataSet ds = new DataSet(); 
     sqa.Fill(ds); 
     DataTable mytab = ds.Tables[0]; 

     GridView1.DataSource = mytab; 
     GridView1.DataBind(); 
     //con.Close(); 

    } 

尋呼

protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e) 
    { 
     GridView1.PageIndex = e.NewPageIndex; 

     bind_grid(); 

    } 

碼的碼用於分選

protected void GridView1_Sorting(object sender, GridViewSortEventArgs e) 
    { 

     DataTable dt = GridView1.DataSource as DataTable; 
     if (dt != null) 
     { 
      DataView dataview = new DataView(dt); 
      dataview.Sort = e.SortExpression + " " + sort_grid(e.SortDirection); 
      GridView1.DataSource = dataview; 
      GridView1.DataBind(); 
     } 
    } 

用戶定義的代碼排序

public string sort_grid() 
    { 
     string newSortDirection = String.Empty; 

     switch (sortDirection) 
     { 
      case SortDirection.Ascending: 
       newSortDirection = "ASC"; 
       break; 

      case SortDirection.Descending: 
       newSortDirection = "DESC"; 
       break; 
     } 

     return newSortDirection; 
    } 

尋呼作品,錯誤是:

1. "no overload for method 'sort_grid' takes 1 argument" (dataview.Sort = e.SortExpression + " " + sort_grid(e.SortDirection);) 

2.The name 'sortDirection does not exist in the current context. (switch (sortDirection)) 

幫助我的朋友。

回答

0

這種方法:

public string sort_grid() 

不帶任何參數,但你試圖用e.SortDirection稱呼它:

dataview.Sort = e.SortExpression + " " + sort_grid(e.SortDirection); 

必須sort_grid()的簽名更改爲

sort_grid(SortDirection sortDirection) 

這也將解決你的第二個問題,你正在嘗試使用sortDirection變量,然後在sort_grid方法中聲明它。