ASP.NET网格视图排序(不使用任何数据库)

时间:2014-03-02 04:18:40

标签: c# asp.net

我在asp.net中遇到了数据网格视图的问题。代码如下:

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

public partial class Lab2_TicketsView : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        lblViewTicketCategoryError.Visible = false;
        lblViewTicketUserError.Visible = false;

        if(!IsPostBack)
        {
            string empty = "";
            string defaultOption = "All";

            ddlTicketViewCategory.Items.Add(empty);
            ddlTicketViewUsers.Items.Add(defaultOption);

            foreach(Category c in CategoryUtility.Instance.GetCategories())
            {
                ddlTicketViewCategory.Items.Add(c.Name);
            }

            var Users = from usr in UserUtility.Instance.GetUsers()
                        select new
                        {
                            Name = usr.FirstName + " " + usr.LastName,
                            usr.Email
                        };

            ddlTicketViewUsers.DataTextField = "Name";
            ddlTicketViewUsers.DataValueField = "Email";
            ddlTicketViewUsers.DataSource = Users;
            ddlTicketViewUsers.DataBind();

            showTicketList();
        }
    }

    protected void showTicketList()
    {
        var Tickets = from ticket in TicketsUtility.Instance.GetTickets()
                      select new
                      {
                          ticket.Title,
                          Name = ticket.User.FirstName + " " + ticket.User.LastName,
                          ticket.Cateogry,
                          ticket.Description
                      };

        dgvTickets.DataSource = Tickets;
        dgvTickets.DataBind();
    }
    protected void btnFilter_Click(object sender, EventArgs e)
    {
        if(ddlTicketViewCategory.SelectedItem.Text == "")
        {
            lblViewTicketCategoryError.Visible = true;
            lblViewTicketCategoryError.Text = "Please select a category";
        }
        else if(ddlTicketViewUsers.SelectedItem.Text == "")
        {
            lblViewTicketUserError.Visible = true;
            lblViewTicketUserError.Text = "Please select a user";
        }
        else
        {
            showTicketList();
        }

    }
}

输出显示在网格视图中。我正在尝试根据从下拉列表中选择的选项对列表进行排序。没有使用数据库,只提供来自提供的库的文本文件和方法(CategoryUtility和UserUtility来自此库)。如何使网格视图中显示的数据基于下拉列表中选择的选项? SelectedItem.Selected会发挥任何一种作用吗?我是否使用LINQ,如果是,请在何处以及如何使用?任何输入都将非常感激。

1 个答案:

答案 0 :(得分:0)

这是为了过滤一个,你也可以为其他过程应用相同的过程,

            if (ddlTicketViewCategory.SelectedItem != null)
            {
                var FilteredList = Tickets.Where(Filter => Filter.Category == ddlTicketViewCategory.SelectedItem.ToString()).ToList();
            }
            dgvTickets.DataSource = FilteredList;
            dgvTickets.DataBind();