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