By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,269 Members | 1,507 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,269 IT Pros & Developers. It's quick & easy.

Sorting on a Gridview

P: n/a
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
Share this Question
Share on Google+
5 Replies


P: n/a
"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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.