473,407 Members | 2,598 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

Sorting on a Gridview

I'm getting my data from an XML file. The data binds fine, but I can't sort
on it. Do I have to do anything special? I have enabled sorting on my grid
view. Autogenerate columns is off, and I use bound fields. The sort
expression is just the same datafield that binds the column, but when I try
to sort with the post back, nothing works.
Jan 9 '07 #1
5 5954
"Cindy Lee" <ci******@hotmail.comwrote in message
news:el*************@TK2MSFTNGP06.phx.gbl...
I'm getting my data from an XML file. The data binds fine, but I can't
sort
on it. Do I have to do anything special? I have enabled sorting on my
grid
view. Autogenerate columns is off, and I use bound fields. The sort
expression is just the same datafield that binds the column, but when I
try
to sort with the post back, nothing works.
You'll need to post your code...
Jan 9 '07 #2
I tried to do a sorting function like I saw on a link, but that didn't work
either. I don't have a datasource to work with, I'm just binding and XML
file, but the following should work.

This is my gridView dec, on the aspx page
<asp:GridView ID="GridView1" runat="server" AllowSorting="True"
AutoGenerateColumns="False" OnSorting="gridView_Sorting" >
<Columns>
<asp:BoundField DataField="name" HeaderText="Name"
ReadOnly="True" SortExpression="name" />
<asp:BoundField DataField="size" HeaderText="Size"
SortExpression="size" />
<asp:BoundField DataField="code" SortExpression="code" />
</Columns>

</asp:GridView>

This is my code on the cs page:
protected void Page_Load(object sender, EventArgs e)
{

String myxml =
"<countries>\n<country>\n<name>ANGOLA</name><code>24</code><size>1345
amp</size>\n";
myxml = myxml +
"</country>\n<country>\n<name>BENIN</name><code>204</code><size>435
amp</size>\n</country>\n</countries>";

DataSet aDataSet = new DataSet();
aDataSet.ReadXml(new StringReader(myxml));

// Bind the DataSet to the grid view

GridView1.DataSource = aDataSet;
GridView1.AllowSorting = true;

GridView1.DataBind();

}

private string ConvertSortDirectionToSql(SortDirection sortDirection)
{
string newSortDirection = String.Empty;

switch (sortDirection)
{
case SortDirection.Ascending:
newSortDirection = "ASC";
break;

case SortDirection.Descending:
newSortDirection = "DESC";
break;
}

return newSortDirection;
}
protected void gridView_Sorting(object sender, GridViewSortEventArgs e)
{
DataTable dataTable = GridView1.DataSource as DataTable;

if (dataTable != null)
{
DataView dataView = new DataView(dataTable);
dataView.Sort = e.SortExpression + " " +
ConvertSortDirectionToSql(e.SortDirection);

GridView1.DataSource = dataView;
GridView1.DataBind();
}
}

"Mark Rae" <ma**@markNOSPAMrae.comwrote in message
news:uB**************@TK2MSFTNGP02.phx.gbl...
"Cindy Lee" <ci******@hotmail.comwrote in message
news:el*************@TK2MSFTNGP06.phx.gbl...
I'm getting my data from an XML file. The data binds fine, but I can't
sort
on it. Do I have to do anything special? I have enabled sorting on my
grid
view. Autogenerate columns is off, and I use bound fields. The sort
expression is just the same datafield that binds the column, but when I
try
to sort with the post back, nothing works.

You'll need to post your code...


Jan 9 '07 #3
I think I'm using a dataSet instead of a dataTable, and that's the problem.
How do I fix that from an XML file?
"Mark Rae" <ma**@markNOSPAMrae.comwrote in message
news:uB**************@TK2MSFTNGP02.phx.gbl...
"Cindy Lee" <ci******@hotmail.comwrote in message
news:el*************@TK2MSFTNGP06.phx.gbl...
I'm getting my data from an XML file. The data binds fine, but I can't
sort
on it. Do I have to do anything special? I have enabled sorting on my
grid
view. Autogenerate columns is off, and I use bound fields. The sort
expression is just the same datafield that binds the column, but when I
try
to sort with the post back, nothing works.

You'll need to post your code...


Jan 9 '07 #4
Or, how do I convert my XML file into a dataTable? Is there an easy way to
do it?

"Cindy Lee" <ci******@hotmail.comwrote in message
news:Os**************@TK2MSFTNGP03.phx.gbl...
I think I'm using a dataSet instead of a dataTable, and that's the
problem.
How do I fix that from an XML file?
"Mark Rae" <ma**@markNOSPAMrae.comwrote in message
news:uB**************@TK2MSFTNGP02.phx.gbl...
"Cindy Lee" <ci******@hotmail.comwrote in message
news:el*************@TK2MSFTNGP06.phx.gbl...
I'm getting my data from an XML file. The data binds fine, but I
can't
sort
on it. Do I have to do anything special? I have enabled sorting on
my
grid
view. Autogenerate columns is off, and I use bound fields. The sort
expression is just the same datafield that binds the column, but when
I
try
to sort with the post back, nothing works.
You'll need to post your code...


Jan 9 '07 #5
Hello Cindy,

Have u tried to google before asking?
http://www.google.ru/search?q=sort+gridview and first two links give u an
answer

CLI'm getting my data from an XML file. The data binds fine, but I
CLcan't sort on it. Do I have to do anything special? I have enabled
CLsorting on my grid view. Autogenerate columns is off, and I use
CLbound fields. The sort expression is just the same datafield that
CLbinds the column, but when I try to sort with the post back, nothing
CLworks.
CL>
---
WBR,
Michael Nemtsev [C# MVP] :: blog: http://spaces.live.com/laflour

"The greatest danger for most of us is not that our aim is too high and we
miss it, but that it is too low and we reach it" (c) Michelangelo
Jan 10 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: ck388 | last post by:
For some reason when I enable the callback feature of the gridview I still get a page refresh, that is it seems like there is a postback that occurs, not a callback which is just supposed to update...
2
by: Arjen | last post by:
Hi, I get this error message when sorting a gridview: The GridView 'GridView1' fired event Sorting which wasn't handled What do I need to do? Thanks!
4
by: samb | last post by:
When I use manual databinding to a GridView control, as bellow. 'Retrive a DataSet from database Dim ds As DataSet = uda.GetUsers(conectionString) 'gvUsers - The GridView gvUsers.DataSource...
4
by: kurt sune | last post by:
I have a an aspx page with a gridview. The gridview is data bound to a generic list of custom classes. The gridview's DataSource is thus not set. Now I want to add sorting to it. So I create...
0
by: jobo | last post by:
Hey there, I'm having a problem getting sorting to work. Here's what the GridView looks like: "server" ID="updt1" Mode="Conditional">
2
by: sivagururaja | last post by:
Hi All, How can i sorting the Gridview Columns via the code behind. When i tried to sorting the column it doesn't work. SqlConnection con = new SqlConnection("Connection string");...
4
by: =?Utf-8?B?R2VyaGFyZA==?= | last post by:
I have a vb.net 2.0 app that is loading a GridView with a DataSource that is returned from a function. The definitions in the function are: Dim ReportDS As DataSet = New DataSet Dim...
3
by: =?Utf-8?B?YmJkb2J1ZGR5?= | last post by:
I have a question that maybe somebody can help me out. I have a gridview that is bound to a sqltable, and I have created two template columns. I am having problems getting the sorting to work....
0
by: =?Utf-8?B?QVZM?= | last post by:
Hi, I've a requirement in which I want to use the custom sorting.. I mean I've a dropdown with sortable column names and when the user selects the particular column from dropdown , I need to sort...
1
by: dorandoran | last post by:
The sort on the childgrid is not working; nothing happens when I click on the each column header for sort. (I followed Satay's sample: http://www.codeproject.com/KB/aspnet/EditNestedGridView.aspx)...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.