2

Im looking for some guidance on being able to sort my current gridview (populated in code behind using linq). The gridview resides on a "results.aspx" page, and a number of different queries run to populate this gridview based on the querystring parameters passed from the page before.

Could someone possible look at my code (forgive me if its not particularly "elegant") and try point me in the right direction as to how i could possibly implement sorting on this?

The results.aspx page only has a couple of labels to accept querystring parameters, and a gridview called viewallGrid.

results.aspx.cs page:

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

    }










}

}

drac
  • 347
  • 2
  • 7
  • 15

1 Answers1

0

The most common solutions to this are to use Session or ViewState and manage the ascending or descending state of the GridView, along with which column was last sorted.

Also, there is an option to associate attributes to the GridView itself, which is useful if you have multiple GridViews on the same page.

Look at the answers to GridView sorting: SortDirection always Ascending for examples of how to do this.

Community
  • 1
  • 1
Karl Anderson
  • 34,606
  • 12
  • 65
  • 80