2

我正在寻找一些关于能够对我当前的 gridview 进行排序的指导(使用 linq 填充在后面的代码中)。gridview 驻留在“results.aspx”页面上,许多不同的查询会根据之前页面传递的查询字符串参数运行以填充此 gridview。

有人可以看看我的代码(如果它不是特别“优雅”,请原谅我)并尝试指出我如何可能实现排序的正确方向?

results.aspx 页面只有几个标签来接受查询字符串参数,以及一个名为 viewallGrid 的网格视图。

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)
    {

    }










}

}

4

1 回答 1

0

最常见的解决方案是使用SessionorViewState和管理 的升序或降序状态GridView,以及最后排序的列。

此外,还有一个将属性关联到自身的选项,如果您在同一页面上GridView有多个 s,这很有用。GridView

查看GridView 排序的答案:SortDirection always Ascending以了解如何执行此操作的示例。

于 2013-08-01T15:09:16.407 回答