Sorting on a Gridview

  • Thread starter Thread starter Cindy Lee
  • Start date Start date
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.
 
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...
 
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();
}
}
 
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?
 
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
 
Back
Top