下拉列表排序问题 [英] Dropdownlist sorting problem

查看:75
本文介绍了下拉列表排序问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,亲爱的朋友,

下拉列表面临问题,请帮助

我会让你清楚这个问题,

我的数据库(sqlserver 2005)中有大量数据,并且在我的网页上使用Gridview显示数据....我在gridview旁边有Dropdownlist

当我从下拉列表中选择按学生姓名排序"时,它在gridview的第一个页面上正确显示,但是在其他页面中,则没有按顺序显示.

因此,我的要求是应按字母顺序显示a-z.并且也在gridview中使用分页.当我从下拉列表中选择"sortby studentname"时,它应按字母顺序显示在gridview的每一页上.当我单击分页时.

而且我还有下拉菜单中的另一个选项,即"SORT BY DATE"
这也应该是相同的...当涉及到分页时.

请从11天开始尝试提供帮助.请解决此问题,并给我一个好的解决方案


这是我的代码
================

Hello My Dear Friends,

am facing problem with dropdown list , Please help

I will make you clear about the problem,

I have large number of data in my Database(sqlserver 2005) and on my webpage am using Gridview to display the data.... its fine. I have Dropdownlist beside gridview

when iam selecting " Sort by studentName" from dropdown list, it is displaying correctly on fist page of gridview, but in other pages it is not displaying in order wise.

So, my requirement is it should display in alphabetical order a-z. and also am using paging in gridview. WHEN I Select "sortby studentname" from dropdownlist it should display in alphabetical order for every page of gridview. when i clicks on paging.

and also i have another option from dropdown as "SORT BY DATE"
This is also should be same...when come to paging.

Please help, trying from 11 days. Please solve this and give me a good solution


this is my code
================

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {

SqlConnection con = new SqlConnection(_connString);
        using (con)
        {
            con.Open();
            DataSet ds = new DataSet();
            if (DropDownList1.SelectedItem.Value == "Sort by StudentName")
            {
                SqlDataAdapter dad = new SqlDataAdapter("select RollNo, StudentId, StudentName, StudFatherName,DateLastChange, DateValidFrom,"
                    + "DateValidTo, ReservationNo, ClassType, location from StudentData order by StudentName", con);
                dad.Fill(ds, "records");
                GridView1.DataSource = ds.Tables[0];
                GridView1.DataBind();

            }

else if (DropDownList1.SelectedItem.Value == "Sort By Date")
            {


SqlDataAdapter dad = new SqlDataAdapter("select RollNo, StudentId, StudentName, StudFatherName,DateLastChange, DateValidFrom,"
                    + "DateValidTo, ReservationNo, ClassType, location from StudentData order by DateLastChange", con);
                
                dad.Fill(ds, "records");
                GridView1.DataSource = ds.Tables[0];
                GridView1.DataBind();

            }





谢谢.





THANKS IN ADVANCE.

推荐答案

您会遇到此问题,因为您正在对dropdwon列表中选定的索引更改事件进行gridview排序.
因此,当您当时在页面索引更改事件中使用页面调度时,您将重新绑定gridview.我说的对吗?

因此,当您在页面索引更改中重新绑定网格时,它将在没有排序标准的情况下绑定网格.因此,您需要在页面索引更改事件中修改用于使用排序条件绑定网格的代码.


这样你就可以走了.

1)创建on函数,该函数将为您提供排序表达式并将其存储在您的视图状态或根据您的要求的任何其他内容中.除了可以保持排序方向外,还可以使用视图状态或其他方式来维护它.
2)因此,在绑定网格时,您可以将该视图状态值用于排序表达式以及排序方向.
3)因此,当您触发下拉列表选择的索引更改以按学生姓名排序时,还要将排序状态的视图状态值设置为学生姓名,并将排序方向设置为ascdesc.
4)当您要更改页面索引时,请使用先前存储的排序表达式和方向来对数据视图进行排序,以保持排序.

您可以使用data-table中的Default View并在该视图上应用排序,而不是在直接查询中使用按原因排序,这对于gridview排序是一个更好的主意.

您可以参考此链接...
排序Gridview [排序GridView 2 [
You face this problem because, you are sorting your gridview on your dropdwon list selected index change event.

so when you using paging at that time in page index change event you are rebinding your gridview. am i right.?

so while you rebind your grid in page index change it will bind the grid with out sorting criteria. so you need to modify code for binding grid with sorted criteria in your page index change event.


so you can go like this.

1) create on function which will give you sort expression and store it in your view state or any thing else as per your requirement. As well as it will also maintain the sort direction, you can also maintain it with view state or other way.
2) so while binding grid you can use that view state value for sort expression as well as sort direction.
3) so while you fire your dropdown selected index change for sorting by student name then also set the value of view state for sort expression as student name and sort direction as asc or desc.
4) while you are going to change your page index as that time sort your data view using previously stored sort expression and direction to maintain sorting.

Instead of using order by cause in direct query you can use Default View of data-table and apply Sorting on that view, it is better idea for gridview sorting.

you can refer this links...
Sorting Gridview[^]
Sorting GridView 2[^]


尝试一下:

您可以使用Grid的pageindexChanging事件.
在那种情况下,您可以写:

Try this :

You can use pageindexChanging event of Grid.
in that event you can write:

protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
   {
        GridView1.PageIndex = e.NewPageIndex;
SqlConnection con = new SqlConnection(_connString);
        using (con)
        {
            con.Open();
            DataSet ds = new DataSet();
            if (DropDownList1.SelectedItem.Value == "Sort by StudentName")
            {
                SqlDataAdapter dad = new SqlDataAdapter("select RollNo, StudentId, StudentName, StudFatherName,DateLastChange, DateValidFrom,"
                    + "DateValidTo, ReservationNo, ClassType, location from StudentData order by StudentName", con);
                dad.Fill(ds, "records");
                GridView1.DataSource = ds.Tables[0];
                GridView1.DataBind();
 
            }
 
else if (DropDownList1.SelectedItem.Value == "Sort By Date")
            {
 

SqlDataAdapter dad = new SqlDataAdapter("select RollNo, StudentId, StudentName, StudFatherName,DateLastChange, DateValidFrom,"
                    + "DateValidTo, ReservationNo, ClassType, location from StudentData order by DateLastChange", con);
                
                dad.Fill(ds, "records");
                GridView1.DataSource = ds.Tables[0];
                GridView1.DataBind();
           }



这将为您提供帮助,否则请发布它.



this will help you, if not please post it...


<div>
            <asp:dropdownlist id="drp" runat="server" autopostback="true" onselectedindexchanged="drp_SelectedIndexChanged" xmlns:asp="#unknown">
                <asp:listitem text="orderid" value="1"></asp:listitem>
                <asp:listitem text="ContactName" value="2"></asp:listitem>
            </asp:dropdownlist>
        </div>
        <div id="my_box">
            <asp:gridview id="GridView1" runat="server" horizontalalign="Justify" autogeneratecolumns="False" xmlns:asp="#unknown">
                Width="100%" AllowPaging="True" PageSize="10" AllowSorting="True" OnPageIndexChanging="GridView1_PageIndexChanging"
                CssClass="mGrid" PagerStyle-CssClass="pgr" AlternatingRowStyle-CssClass="alt">
                <pagersettings position="TopAndBottom" firstpagetext="First" lastpagetext="Last">
                    NextPageText="Next" PreviousPageText="Previous" />
                <rowstyle horizontalalign="Center" verticalalign="Middle" font-names="Sans-Serif,Arial">
                    Font-Size="Small" />
                <columns>
                    <asp:boundfield datafield="OrderId" headertext="Job Ref" readonly="True" sortexpression="OrderId" />
                    <asp:boundfield datafield="Typeofwork" headertext="Language" readonly="True" sortexpression="Typeofwork" />
                    <asp:boundfield datafield="dateofmeeting" headertext="Job Date" readonly="True" sortexpression="dateofmeeting">
                        DataFormatString="{0:dd/MM/yyyy}" />
                    <asp:boundfield datafield="timeofmeeting" headertext="Job Time" readonly="True" sortexpression="timeofmeeting">
                        DataFormatString="{0:t}" />
                    <asp:boundfield datafield="Locationofmeeting" headertext="Location of Meeting" readonly="True">
                        SortExpression="Locationofmeeting" />
                    <%--<asp:boundfield datafield="Client(family)name" headertext="Client Name" readonly="True">
                        SortExpression="Client(family)name" />
                    <asp:boundfield datafield="ordertaken" headertext="Booking Submitted On" readonly="True">
                        SortExpression="ordertaken" DataFormatString="{0:dd/MM/yyyy HH:mm}" HtmlEncode="False">
                        <headerstyle horizontalalign="Left" />
                    </asp:boundfield>--%>
                    <asp:boundfield datafield="ContactName" headertext="Contact Name" readonly="True">
                        SortExpression="ContactName" />
                    <%--<asp:templatefield headertext="Interpreter Name" sortexpression="FirstName">
                        <edititemtemplate>
                            <asp:label id="Label1" runat="server" text="<%# Eval("InterpreterName") %>"></asp:label>
                        </edititemtemplate>
                        <itemtemplate>
                            <asp:label id="Label1" runat="server" text="<%# Bind("FirstName") %>"></asp:label>
                            <asp:label id="Label2" runat="server" text="<%# Eval("LastName") %>"></asp:label>
                        </itemtemplate>
                    </asp:templatefield>
                    <asp:templatefield headertext="Cancelled">
                        <itemtemplate>
                            <asp:label id="lblcancelled" runat="server" text="<%#Bind("Cancelled")%>"></asp:label>
                        </itemtemplate>
                    </asp:templatefield>
                    <asp:templatefield headertext="View">
                        <itemtemplate>
                            <asp:hyperlink id="ViewLink" runat="server" target="_self" text="View" navigateurl="<%# "Viewbooking.aspx?OrderId=" + DataBinder.Eval(Container, "DataItem.OrderId")%>">
                                ForeColor="Blue">
                            </asp:hyperlink>
                        </itemtemplate>
                    </asp:templatefield>--%>
                </asp:boundfield></asp:boundfield></asp:boundfield></asp:boundfield></asp:boundfield></columns>
                <pagerstyle cssclass="pgr" backcolor="Transparent" bordercolor="Transparent" font-underline="False" />
                <headerstyle font-bold="True" font-names="Sans-Serif,Arial" font-size="Small" forecolor="White">
                    Height="30px" HorizontalAlign="Left" BorderStyle="None" />
                <alternatingrowstyle cssclass="alt" />
            </headerstyle></rowstyle></pagersettings></asp:gridview>
        </div>





using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
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;

public partial class _Default : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["Citas"]);
    SqlCommand cmd1 = new SqlCommand();
    public string custid;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {

            bindgrid("orderid", "DESC");
        }

    }
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        bindgrid(drp.SelectedItem.Text, null);

    }
    protected void drp_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (drp.SelectedItem.Text == "orderid")
        {
            bindgrid("orderid", "DESC"); 
        }
        else if (drp.SelectedItem.Text == "ContactName")
        {
            bindgrid("ContactName", "DESC"); 
        }
    }
    private void bindgrid(string sortExpression, string direction)
    {
        SqlDataAdapter da = new SqlDataAdapter("select top 100 * from Requests", con);
        DataTable dt = new DataTable();


        da.Fill(dt);
        DataView dv = new DataView(dt);
        if (IsPostBack)
        {
            dv.Sort = sortExpression + " " + direction;
        }


        GridView1.DataSource = dv;
        GridView1.DataBind();
        //DataSet ds = selectalljobsbydate(custid);
        //try
        //{
        //    GridView1.DataSource = dt;
        //    GridView1.DataBind();
        //}
        //catch (Exception ex)
        //{
        //    //lblMessage.Text = ex.Message;
        //    //lblMessage.ForeColor = Color.Red;
        //}
        //finally
        //{

        //    con.Close();
        //}

    }
}





希望它对您有用.....

kkakadiya ....





hope it will work for you.....

kkakadiya....


这篇关于下拉列表排序问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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