Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
|
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old May 28th, 2008, 12:34 PM
Authorized User
 
Join Date: Apr 2008
Posts: 16
Thanks: 1
Thanked 0 Times in 0 Posts
Default GridView Sorting

Hi,

I have a gridview which I want to sort it. But I have a problem with the code. You have any idea what it's wrong?



 <asp:GridView ID="gvUserList" ForeColor="red" AutoGenerateColumns="false" runat="server" OnRowCommand="gvUserList_RowCommand" OnRowDataBound="gvUserList_RowDataBound" AllowPaging="true" AllowSorting="true" OnPageIndexChanging="gvUserList_PageIndexChanging" OnSorting="gvUserList_PageSorting" >
            <Columns>
            <asp:BoundField DataField="UserID" Visible="false" />

            <asp:TemplateField >
                 <HeaderTemplate >
                     <asp:LinkButton ID="lblUserName" runat="server">UserName</asp:LinkButton>
                 </HeaderTemplate>
                 <ItemTemplate>
                      <asp:LinkButton ID="lnkUserName" runat="server" CommandName="EditUser" ForeColor="red" Text='<%# Bind("UserName") %>'> </asp:LinkButton>
                 </ItemTemplate>
            </asp:TemplateField>

            <asp:BoundField DataField="FirstName" HeaderText="First Name" />
            <asp:BoundField DataField="LastName" HeaderText="Last Name" />
            <asp:BoundField DataField="Age" HeaderText="Age" />
            <asp:BoundField DataField="Email" HeaderText="Email" />
            <asp:BoundField DataField="Inactive" HeaderText="Status" Visible="false"/>

            <asp:TemplateField >
                 <ItemTemplate>
                        <asp:ImageButton ID="imgbtnSetInactiveUser" runat="server" ImageUrl="~/Images/inactive.jpg" CommandName="SetUser" />
                 </ItemTemplate>
            </asp:TemplateField>
            </Columns>

            </asp:GridView>


 protected void gvUserList_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {

            gvUserList.PageIndex = e.NewPageIndex;
            gvUserList.DataBind();


        }



        protected void gvUserList_PageSorting(object sender, GridViewSortEventArgs e)
        {

            DataTable objDT = gvUserList.DataSource as DataTable;

            if (objDT != null)
            {

                DataView objDV = new DataView(objDT);
                objDV.Sort = e.SortExpression + " " + SqlSortDirection(e.SortDirection);

                gvUserList.DataSource = objDV;
                gvUserList.DataBind();


            }


        }


        private string SqlSortDirection(SortDirection sortDirection)
        {

            string newSortDirection = string.Empty;

            if (sortDirection == SortDirection.Ascending)
            {
                newSortDirection = "ASC";
            }

            else if ( sortDirection == SortDirection.Descending)
            {
                newSortDirection = "DESC";
            }


            return newSortDirection;

        }









Thank you
__________________
Thank you
 
Old May 28th, 2008, 03:18 PM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

You don't actually say what the problem is?

/- Sam Judson : Wrox Technical Editor -/
 
Old May 29th, 2008, 04:45 AM
Authorized User
 
Join Date: Apr 2008
Posts: 16
Thanks: 1
Thanked 0 Times in 0 Posts
Default


The problem is that the link button appears in the gridview list, but when I click on it, nothing happens .(it should sort my column, but something it's wrong.)

Even If I add "Sort Expression" to <asp:BoundField> tag, sorting is not working.


Thank you
 
Old May 29th, 2008, 05:01 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

Code:
DataTable objDT = gvUserList.DataSource as DataTable;
This line here assumes that the DataSource is a DataTable - yet in the very next bit of code you assign a DataView to DataSource. I suspect this will be where your problem lies.

/- Sam Judson : Wrox Technical Editor -/
 
Old May 29th, 2008, 05:10 AM
Authorized User
 
Join Date: Apr 2008
Posts: 16
Thanks: 1
Thanked 0 Times in 0 Posts
Default

I need that DataView for sorting. DataView is view of a DataTable , and I use it for sorting, searching, editing.

I cannot use Sort property directly with DataTable.

Anyway , is a DataView of a DataTable : DataView objDV = new DataView(objDT);

I think another line is my problem.

Thank you
 
Old May 29th, 2008, 05:17 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

I realise you have to use DataView, but if you assign a DataView to the DataSource of your GridView then in the above line objDT will be null.

/- Sam Judson : Wrox Technical Editor -/
 
Old May 29th, 2008, 07:22 AM
Authorized User
 
Join Date: Apr 2008
Posts: 16
Thanks: 1
Thanked 0 Times in 0 Posts
Default

Sam, can you give me one suggestion , please ?



Thank you
 
Old May 29th, 2008, 07:50 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

I've already highlighted the area I think you should be investigating. I really don't know what else to say.

/- Sam Judson : Wrox Technical Editor -/





Similar Threads
Thread Thread Starter Forum Replies Last Post
Sorting and Paging of gridview lily611 C# 2005 0 April 24th, 2008 03:09 AM
GridView sorting error snufse ASP.NET 2.0 Basics 0 February 12th, 2008 06:00 PM
SORTING IN GRIDVIEW santosh.kundlik ASP.NET 2.0 Professional 0 March 22nd, 2007 03:52 AM
Sorting GridView DARSIN ASP.NET 2.0 Basics 0 December 14th, 2004 09:36 AM
Re:Sorting gridview m_rajib .NET Framework 2.0 2 December 14th, 2004 08:50 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.