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

May 28th, 2008, 12:34 PM
|
|
Authorized User
|
|
Join Date: Apr 2008
Posts: 16
Thanks: 1
Thanked 0 Times in 0 Posts
|
|
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
|
|

May 28th, 2008, 03:18 PM
|
 |
Friend of Wrox
|
|
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
|
|
You don't actually say what the problem is?
/- Sam Judson : Wrox Technical Editor -/
|
|

May 29th, 2008, 04:45 AM
|
|
Authorized User
|
|
Join Date: Apr 2008
Posts: 16
Thanks: 1
Thanked 0 Times in 0 Posts
|
|
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
|
|

May 29th, 2008, 05:01 AM
|
 |
Friend of Wrox
|
|
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
|
|
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 -/
|
|

May 29th, 2008, 05:10 AM
|
|
Authorized User
|
|
Join Date: Apr 2008
Posts: 16
Thanks: 1
Thanked 0 Times in 0 Posts
|
|
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
|
|

May 29th, 2008, 05:17 AM
|
 |
Friend of Wrox
|
|
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
|
|
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 -/
|
|

May 29th, 2008, 07:22 AM
|
|
Authorized User
|
|
Join Date: Apr 2008
Posts: 16
Thanks: 1
Thanked 0 Times in 0 Posts
|
|
Sam, can you give me one suggestion , please ?
Thank you
|
|

May 29th, 2008, 07:50 AM
|
 |
Friend of Wrox
|
|
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
|
|
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 -/
|
|
 |