Sorting on a Gridview

C

Cindy Lee

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

Mark Rae

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

Cindy Lee

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();
}
}
 
C

Cindy Lee

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

Cindy Lee

Or, how do I convert my XML file into a dataTable? Is there an easy way to
do it?
 
M

Michael Nemtsev

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

CL> I'm getting my data from an XML file. The data binds fine, but I
CL> can't sort on it. Do I have to do anything special? I have enabled
CL> sorting on my grid view. Autogenerate columns is off, and I use
CL> bound fields. The sort expression is just the same datafield that
CL> binds the column, but when I try to sort with the post back, nothing
CL> works.
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) Michelangel
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top