473,654 Members | 3,108 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 5968
"Cindy Lee" <ci******@hotma il.comwrote in message
news:el******** *****@TK2MSFTNG P06.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="T rue"
AutoGenerateCol umns="False" OnSorting="grid View_Sorting" >
<Columns>
<asp:BoundFie ld DataField="name " HeaderText="Nam e"
ReadOnly="True" SortExpression= "name" />
<asp:BoundFie ld DataField="size " HeaderText="Siz e"
SortExpression= "size" />
<asp:BoundFie ld DataField="code " SortExpression= "code" />
</Columns>

</asp:GridView>

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

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

DataSet aDataSet = new DataSet();
aDataSet.ReadXm l(new StringReader(my xml));

// Bind the DataSet to the grid view

GridView1.DataS ource = aDataSet;
GridView1.Allow Sorting = true;

GridView1.DataB ind();

}

private string ConvertSortDire ctionToSql(Sort Direction sortDirection)
{
string newSortDirectio n = String.Empty;

switch (sortDirection)
{
case SortDirection.A scending:
newSortDirectio n = "ASC";
break;

case SortDirection.D escending:
newSortDirectio n = "DESC";
break;
}

return newSortDirectio n;
}
protected void gridView_Sortin g(object sender, GridViewSortEve ntArgs e)
{
DataTable dataTable = GridView1.DataS ource as DataTable;

if (dataTable != null)
{
DataView dataView = new DataView(dataTa ble);
dataView.Sort = e.SortExpressio n + " " +
ConvertSortDire ctionToSql(e.So rtDirection);

GridView1.DataS ource = dataView;
GridView1.DataB ind();
}
}

"Mark Rae" <ma**@markNOSPA Mrae.comwrote in message
news:uB******** ******@TK2MSFTN GP02.phx.gbl...
"Cindy Lee" <ci******@hotma il.comwrote in message
news:el******** *****@TK2MSFTNG P06.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**@markNOSPA Mrae.comwrote in message
news:uB******** ******@TK2MSFTN GP02.phx.gbl...
"Cindy Lee" <ci******@hotma il.comwrote in message
news:el******** *****@TK2MSFTNG P06.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******@hotma il.comwrote in message
news:Os******** ******@TK2MSFTN GP03.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**@markNOSPA Mrae.comwrote in message
news:uB******** ******@TK2MSFTN GP02.phx.gbl...
"Cindy Lee" <ci******@hotma il.comwrote in message
news:el******** *****@TK2MSFTNG P06.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
2727
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 not the whole page, but a portion of the page. Strangely enough the URL below http://beta.asp.net/QUICKSTARTV20/aspnet/doc/ctrlref/data/gridview.aspx (VB GridView Paging and Sorting Callbacks example)
2
11116
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
8242
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 = ds gvUsers.DataBind()
4
5476
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 an eventhandler thus: Protected Sub grdResult_Sorting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewSortEventArgs) Handles grdResult.Sorting
0
1758
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
5578
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"); con.Open(); SqlCommand cmd = new SqlCommand(str, con); SqlDataAdapter da = new SqlDataAdapter(cmd);
4
11147
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 ReportTable As System.Data.DataTable = New System.Data.DataTable("SendTo") The ReportTable is populated row by row by data gotten back from the
3
22750
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. I turned on the Allow Sorting property but when I click one of the columns that is bound, it will not sort. Below is the code I am using
0
1215
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 the grid. For it,Im trying to get the gridView.DataSource() to retrieve the datatable attached to it (Ive earlier binded the gridview in page load in not postback section ) , but gridView.DataSource() always returning null.. Im unable to...
1
2946
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) and i am using object for datasource. please suggest. = = = default.aspx = = <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"...
0
8372
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8285
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8814
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8591
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
4149
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4293
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2709
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1915
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1592
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.