c# - 使用 linq to entities c# 的 gridview_sorting 事件

标签 c# asp.net linq gridview linq-to-entities

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

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

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

    }










}

最佳答案

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

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

查看 GridView sorting: SortDirection always Ascending 的答案有关如何执行此操作的示例。

关于c# - 使用 linq to entities c# 的 gridview_sorting 事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17997269/

相关文章:

c# - KeyPress 事件仅在 MonoDroid 中的某些键上触发

c# - 如何使用 asp.net 在 GridView 中为页码加下划线

c# - 从派生类在 powershell 中构建自定义类型

javascript - 浏览器链接和 Web 窗体(设计模式不适用于 ASP.NET Web 窗体)

ASP.NET 内容页面中的 jQuery id 选择

c# - 具有继承类型的 TreeView

.net - 我应该在生产环境中使用 'Integrated Security=True' 吗?

c# - LINQ/C# : Add record isnt working for some reason

c# - LINQ 语句中的 N-Enumerables?

c# - LINQ:如何使用 group by 子句获取 Max Id?