2013-08-01 50 views
2

我正在尋找一些指導,能夠排序我當前的網格視圖(在代碼中使用LINQ填充)。 gridview駐留在「results.aspx」頁面上,並且運行許多不同的查詢以基於從此頁面傳遞的查詢字符串參數填充此gridview。gridview_sorting事件使用LINQ到實體C#

可能有人可以看看我的代碼(如果不是特別「優雅」原諒我),並嘗試點我在正確的方向,以我怎麼可能實現這個排序?

results.aspx頁面只有幾個標籤來接受查詢字符串參數和一個名爲viewallGrid的gridview。

results.aspx.cs頁:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using System.Data; 

namespace WebApplication1 
{ 
public partial class Results : System.Web.UI.Page 
{ 
    public int _manufacturer { get; set; } 
    public int _decibel { get; set; } 
    public int _doortype { get; set; } 
    public int _glazing { get; set; } 
    public int _ref { get; set; } 


    protected void Page_Load(object sender, EventArgs e) 
    { 

     if (!IsPostBack) //FIRST TIME PAGE IS LOADED 
     { 
      hideLabels(decibelLabel, doortypeLabel, glazingLabel, mLabel, refLabel); 
      Initialdecision(); 

     } 

     else 
     { 

     } 
    } 


    protected void Initialdecision() 
    { 
     var refResult = Request.QueryString["sf"]; 
     int idResult = Convert.ToInt32(Request.QueryString["test"]); 
     string prodResult = Request.QueryString["prod"]; 
     if (refResult == "ms") 
     { 
      mastersearchQuery(); 

     } 

     if (refResult == "rs") 
     { 
      refsearchQuery(); 

     } 

     if (refResult == "id") 
     { 
      testidsearchQuery(idResult); 

     } 

     if (refResult == "p") 
     { 
      productsearchQuery(prodResult); 

     } 
    } 
    protected void mastersearchQuery() 
    { 
     _manufacturer = Convert.ToInt32(Request.QueryString["m"]); 
     _decibel = Convert.ToInt32(Request.QueryString["db"]); 
     _doortype = Convert.ToInt32(Request.QueryString["dt"]); 
     _glazing = Convert.ToInt32(Request.QueryString["gl"]); 

     totalLabel.Text = Request.QueryString["total"]; 

     decibelresultLabel.Text = _decibel.ToString(); 
     doortyperesultLabel.Text = _doortype.ToString(); 
     glazingresultLabel.Text = _glazing.ToString(); 
     manufacturerresultLabel.Text = _manufacturer.ToString(); 

     populatemanufacturerLabel(); 
     populatedecibelLabel(); 
     populatedoortypeLabel(); 
     populateglazingLabel(); 

     testdatanewEntities _result = new testdatanewEntities(); 
     var resultQuery = (from t in _result.View3 
          where (t.dbRating >= _decibel && t.AllID == 1) && 
        (t.doortypeID == _doortype || t.AllID == _doortype) && 
        (t.GlazingID == _glazing || t.AllID == _glazing) && 
        (t.ManufactureID == _manufacturer || t.AllID == _manufacturer) 
          orderby t.dbRating descending 
          select new { t.TestID, t.Core, t.Head, t.Threshold, t.MeetingStiles, t.GlazingsType, t.dbRating, t.Pic, t.PDF }); 

     viewallGrid.DataSource = resultQuery; 
     viewallGrid.DataBind(); 
     viewallGrid.UseAccessibleHeader = true; 
     viewallGrid.HeaderRow.TableSection = TableRowSection.TableHeader; 

     TableCellCollection cells = viewallGrid.HeaderRow.Cells; 
     //ID 
     cells[0].Attributes.Add("data-class", "expand"); 
     //CORE 
     //HEAD 
     cells[2].Attributes.Add("data-hide", "phone,tablet"); 
     //THRESHOLD 
     cells[3].Attributes.Add("data-hide", "phone,tablet"); 
     //MEETING 
     cells[4].Attributes.Add("data-hide", "phone,tablet"); 
     //GLAZING 
     cells[5].Attributes.Add("data-hide", "phone,tablet"); 
     //dB 
     //SYSTEM PIC 
     cells[7].Attributes.Add("data-hide", "phone,tablet"); 
     //PDF 
     cells[8].Attributes.Add("data-hide", "phone,tablet"); 
    } 

    protected void productsearchQuery(string a) 
    { 
     refLabel.Text = "You have searched for test data including the product: NOR" + a.ToString(); 
     refLabel.Visible = true; 

     testdatanewEntities _productquery = new testdatanewEntities(); 
     var prodQuery = (from p in _productquery.View3 
         where p.Spare3.Contains(a) 
         orderby p.dbRating descending 
         select new { p.TestID, p.Core, p.Head, p.Threshold, p.MeetingStiles, p.GlazingsType, p.dbRating, p.Pic, p.PDF }); 

     if (prodQuery.Count() > 0) 
     { 
     viewallGrid.DataSource = prodQuery; 
     viewallGrid.DataBind(); 
     viewallGrid.UseAccessibleHeader = true; 
     viewallGrid.HeaderRow.TableSection = TableRowSection.TableHeader; 

     TableCellCollection cells = viewallGrid.HeaderRow.Cells; 
     //ID 
     cells[0].Attributes.Add("data-class", "expand"); 
     //CORE 
     //HEAD 
     cells[2].Attributes.Add("data-hide", "phone,tablet"); 
     //THRESHOLD 
     cells[3].Attributes.Add("data-hide", "phone,tablet"); 
     //MEETING 
     cells[4].Attributes.Add("data-hide", "phone,tablet"); 
     //GLAZING 
     cells[5].Attributes.Add("data-hide", "phone,tablet"); 
     //dB 
     //SYSTEM PIC 
     cells[7].Attributes.Add("data-hide", "phone,tablet"); 
     //PDF 
     cells[8].Attributes.Add("data-hide", "phone,tablet"); 
     } 
     if (prodQuery.Count() == 0) 
     { 
      errorLabel.Text = "The Test ID " + a + " can not be found, please check the ID number and try again"; 
      errorLabel.Visible = true; 
     } 


    } 

    protected void refsearchQuery() 
    { 
     var reftypeResult = Convert.ToInt32(Request.QueryString["ref"]); 

     refLabel.Text = "You have searched for report reference: C/" + reftypeResult.ToString(); 
     refLabel.Visible = true; 

     testdatanewEntities _result = new testdatanewEntities(); 
     var resultQuery = (from t in _result.View3 
          where t.testRef == reftypeResult 
          orderby t.dbRating descending 
          select new { t.TestID, t.Core, t.Head, t.Threshold, t.MeetingStiles, t.GlazingsType, t.dbRating, t.Pic, t.PDF }); 

     if (resultQuery.Count() > 0) 
     { 
      viewallGrid.DataSource = resultQuery; 
      viewallGrid.DataBind(); 
      viewallGrid.UseAccessibleHeader = true; 
      viewallGrid.HeaderRow.TableSection = TableRowSection.TableHeader; 

      TableCellCollection cells = viewallGrid.HeaderRow.Cells; 
      //ID 
      cells[0].Attributes.Add("data-class", "expand"); 
      //CORE 
      //HEAD 
      cells[2].Attributes.Add("data-hide", "phone,tablet"); 
      //THRESHOLD 
      cells[3].Attributes.Add("data-hide", "phone,tablet"); 
      //MEETING 
      cells[4].Attributes.Add("data-hide", "phone,tablet"); 
      //GLAZING 
      cells[5].Attributes.Add("data-hide", "phone,tablet"); 
      //dB 
      //SYSTEM PIC 
      cells[7].Attributes.Add("data-hide", "phone,tablet"); 
      //PDF 
      cells[8].Attributes.Add("data-hide", "phone,tablet"); 

     } 
     if (resultQuery.Count() == 0) 
     { 
      errorLabel.Text = "The report ref " + reftypeResult + " can not be found, please check the reference and try again"; 
      errorLabel.Visible = true; 
     } 

    } 

    protected void testidsearchQuery(int a) 
    { 

     refLabel.Text = "You have searched for the Test ID # " + a.ToString(); 
     refLabel.Visible = true; 

     testdatanewEntities _result = new testdatanewEntities(); 
     var resultQuery = (from t in _result.View3 
          where t.TestID == a 
          orderby t.dbRating descending 
          select new { t.TestID, t.Core, t.Head, t.Threshold, t.MeetingStiles, t.GlazingsType, t.dbRating, t.Pic, t.PDF }); 

     if (resultQuery.Count() > 0) 
     { 
      viewallGrid.DataSource = resultQuery; 
      viewallGrid.DataBind(); 
      viewallGrid.UseAccessibleHeader = true; 
      viewallGrid.HeaderRow.TableSection = TableRowSection.TableHeader; 

      TableCellCollection cells = viewallGrid.HeaderRow.Cells; 
      //ID 
      cells[0].Attributes.Add("data-class", "expand"); 
      //CORE 
      //HEAD 
      cells[2].Attributes.Add("data-hide", "phone,tablet"); 
      //THRESHOLD 
      cells[3].Attributes.Add("data-hide", "phone,tablet"); 
      //MEETING 
      cells[4].Attributes.Add("data-hide", "phone,tablet"); 
      //GLAZING 
      cells[5].Attributes.Add("data-hide", "phone,tablet"); 
      //dB 
      //SYSTEM PIC 
      cells[7].Attributes.Add("data-hide", "phone,tablet"); 
      //PDF 
      cells[8].Attributes.Add("data-hide", "phone,tablet"); 
     } 

     if (resultQuery.Count() == 0) 
     { 
      errorLabel.Text = "The Test ID " + a + " can not be found, please check the ID number and try again"; 
      errorLabel.Visible = true; 
     } 

    } 

    protected void hideLabels(Label a, Label b, Label c, Label d, Label e) 
    { 
     a.Visible = false; 
     b.Visible = false; 
     c.Visible = false; 
     d.Visible = false; 
     e.Visible = false; 
    } 

    protected void populatedecibelLabel() 
    { 
     decibelLabel.Visible = true; 
     testdatanewEntities _decquery = new testdatanewEntities(); 
     var dquery = (from t in _decquery.View3 
         where t.dbRating == _decibel 
         select t.dbRating).FirstOrDefault(); 

     if (_decibel == 1) 
     { 
      decibelLabel.Text = "All dBRw - "; 
     } 
     else 
     { 
      decibelLabel.Text = dquery.ToString() + "dBRw - "; 
     } 

    } 

    protected void populatemanufacturerLabel() 
    { 
     mLabel.Visible = true; 
     testdatanewEntities _manuquery = new testdatanewEntities(); 
     var mquery = (from t in _manuquery.View3 
         where t.ManufactureID == _manufacturer 
         select t.manufacturerName).FirstOrDefault(); 

     if (_manufacturer == 1) 
     { 
      mLabel.Text = "All manufacturers"; 
     } 
     else 
     { 
      mLabel.Text = mquery.ToString(); 
     } 

    } 

    protected void populatedoortypeLabel() 
    { 
     doortypeLabel.Visible = true; 
     testdatanewEntities _doorquery = new testdatanewEntities(); 
     var doorquery = (from t in _doorquery.View3 
         where t.doortypeID == _doortype 
         select t.doortypeName).FirstOrDefault(); 

     if (_doortype == 1) 
     { 
      doortypeLabel.Text = "All door types - "; 
     } 
     else 
     { 
      doortypeLabel.Text = doorquery.ToString() + " - "; 
     } 


    } 

    protected void populateglazingLabel() 
    { 
     glazingLabel.Visible = true; 
     testdatanewEntities _glazingquery = new testdatanewEntities(); 
     var glazingquery = (from t in _glazingquery.View3 
          where (t.glazID == _glazing || t.AllID == _glazing) 
          select t.glazTypeName).FirstOrDefault(); 


     if (_glazing == 1) 
     { 
      glazingLabel.Text = "All glazing - "; 
     } 
     else 
     { 
      glazingLabel.Text = glazingquery.ToString() + " - "; 
     } 
    } 

    //navigate through pages in result grid 
    protected void ResultGrid_PageIndexChanging(object sender, GridViewPageEventArgs e) 
    { 

     viewallGrid.PageIndex = e.NewPageIndex; 
     Initialdecision(); 
     viewallGrid.UseAccessibleHeader = true; 
     viewallGrid.HeaderRow.TableSection = TableRowSection.TableHeader; 

     TableCellCollection cells = viewallGrid.HeaderRow.Cells; 
     //ID 
     cells[0].Attributes.Add("data-class", "expand"); 
     //CORE 
     //HEAD 
     cells[2].Attributes.Add("data-hide", "phone,tablet,desktop"); 
     //THRESHOLD 
     cells[3].Attributes.Add("data-hide", "phone,tablet,desktop"); 
     //MEETING 
     cells[4].Attributes.Add("data-hide", "phone,tablet,desktop"); 
     //GLAZING 
     cells[5].Attributes.Add("data-hide", "phone,tablet,desktop"); 
     //dB 
     //SYSTEM PIC 
     cells[7].Attributes.Add("data-hide", "phone,tablet,desktop"); 
     //PDF 
     cells[8].Attributes.Add("data-hide", "phone,tablet,desktop"); 

    } 

    protected void viewallGrid_Sorting(object sender, GridViewSortEventArgs e) 
    { 

    } 










} 

}

回答

0

的這種最常見的解決方案是使用SessionViewState和管理GridView的上升或下降狀態,與沿列最後排序。

此外,還可以選擇將屬性與GridView本身相關聯,如果在同一頁面上有多個GridView s,則該選項非常有用。

查看GridView sorting: SortDirection always Ascending的答案,瞭解如何執行此操作的示例。

0

好的,謝謝你,我查看了上面的鏈接,然後看了一些例子,但不幸的是我無法得到這個與我當前的代碼集成,我沒有任何辦法要求爲我做這件事,但im忙於學習LINQ的實體,並會很感激,如果有人可以幫助集成gridview_sorting事件處理程序與我目前的編碼?