使用C#向GridView添加排序功能 [英] Add Sort Functionality To GridView Using C#

查看:99
本文介绍了使用C#向GridView添加排序功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个现有的搜索页面,它将使用存储过程搜索SQL DB中的记录。搜索结果将发布到同一页面上的GridView中。我想包括排序功能,因为搜索可以返回多个记录,但我不知道如何将其添加到现有的C#中。 GridView的代码(以及文本框和搜索按钮)目前是:

I have an existing search page that will search records in a SQL DB using a stored procedure. Search results are posted to a GridView on the same page. I would like to include sort functionality because a search can return multiple records, but I'm not sure how to add this to the existing C#. The code for the GridView (as well as text box and search button) is currently:

        <p>Search for someone by name, email address, or employee ID</p>
<!-- Panel sets Button1 as default, to click on Enter key -->    
    <asp:Panel ID="panSearch" runat="server" DefaultButton="Button1">
    <asp:TextBox ID="TextBox1" runat="server" MaxLength="100"></asp:TextBox>
    </asp:Panel>
    <br />
    <asp:Button ID="Button1" runat="server" Text="Search" OnClick="Button1_Click" /><br />
    <br />

<!-- redirect to new URL on Select achieved through asp:hyperlinkfield -->
    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" Width="90%" AlternatingRowStyle-BackColor="#E2E2E2">
        <Columns>
            <asp:HyperLinkField DataNavigateUrlFields="PK" DataNavigateUrlFormatString="PeopleSearch2.aspx?PK={0}" Text="Select"></asp:HyperLinkField>
            <asp:BoundField DataField="FirstName" HeaderText="First" SortExpression="FirstName"></asp:BoundField>
            <asp:BoundField DataField="LastName" HeaderText="Last" SortExpression="LastName"></asp:BoundField>
            <asp:BoundField DataField="Email" HeaderText="Email" SortExpression="Email"></asp:BoundField>
            <asp:BoundField DataField="Manager" HeaderText="Manager" SortExpression="Manager"></asp:BoundField>
            <asp:BoundField DataField="EmpStatus" HeaderText="Status" SortExpression="EmpStatus"></asp:BoundField>
                 

        </Columns>
    </asp:GridView>





我的猜测是,对于GridView,我只需要添加AllowSorting =true和OnSorting =GridView1_Sorting但是我需要将GridView1_Sorting添加到后面的代码中并可能修改我的电子邮件xisting C#。我不想失去任何当前的功能。 C#代码目前是:





My guess is that for the GridView, I would only need to add AllowSorting="true" and OnSorting="GridView1_Sorting" but then I'll need to add GridView1_Sorting to the code behind and possibly modify my existing C#. I don't want to lose any current functionality. The C# code is currently:

using System;
using System.Data;
using System.Configuration;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

namespace Max.Security
{
    public partial class PeopleSearch1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            TextBox1.Focus();  // this sets the cursor to the textbox on page load
            {
                if (Request.QueryString["searchString"] != null)
                {
                    DisplaySearchResults(Request.QueryString["searchString"]);
                }
            }
        }

        public void DisplaySearchResults(string strSearch)
        {
            SqlCommand cmd = new SqlCommand("dbo.aspPeopleSearch", new SqlConnection(ConfigurationManager.AppSettings["ConnString"]));
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@SearchString", strSearch);
            cmd.Connection.Open();

            GridView1.DataSource = cmd.ExecuteReader();
            GridView1.DataBind();

            cmd.Connection.Close();
            cmd.Connection.Dispose();
        }


        protected void Button1_Click(object sender, EventArgs e)  // this allows the Enter key to fire button1
        {
            Response.Redirect("PeopleSearch1.aspx?searchString=" + Server.UrlEncode(TextBox1.Text)); 
        }



    }
}

推荐答案

请阅读以下文章



http:/ /www.mikepope.com/blog/DisplayBlog.aspx?permalink=1418 [ ^ ]
please read below article

http://www.mikepope.com/blog/DisplayBlog.aspx?permalink=1418[^]


请参阅..

http://msdn.microsoft.com/en-us/library/system.web.ui .webcontrols.gridview.sorting(v = vs.110).aspx [ ^ ]

http://www.c-sharpcorner.com/UploadFile/b8e86c/paging-and-sorting-in-Asp -net-GR idview / [ ^ ]

http://www.dotnetgallery.com/kb/resource12-How-to-implement-paging-and-sorting-in-aspnet-Gridview-control.aspx [ ^ ]
See..
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.sorting(v=vs.110).aspx[^]
http://www.c-sharpcorner.com/UploadFile/b8e86c/paging-and-sorting-in-Asp-Net-gridview/[^]
http://www.dotnetgallery.com/kb/resource12-How-to-implement-paging-and-sorting-in-aspnet-Gridview-control.aspx[^]


这篇关于使用C#向GridView添加排序功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆