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