You have to do it programmatically - although the solution is somewhat nasty.
First step is to define DataKeys and onSorting and Sorted events in ListView as below
<asp:ListView ID="ListView1" runat="server" DataSourceID="SqlDataSource1" DataKeyNames="AddressId,AddressLine1"
onsorting="ListView1_Sorting" onsorted="ListView1_Sorted">
Then in the code behind you have to handle the events.Since the DataItems on the Items collection is always null and DataIndex and DisplayIndex are not set as one would normally expect we have to use DataKeys.Store datakey of selected Item before sort and after sort search through DatakEy collection to match with stored datakey. See below
private DataKey dk;
protected void ListView1_Sorting(object sender, ListViewSortEventArgs e)
{
dk= (ListView1.SelectedIndex > 0) ? ListView1.DataKeys[ListView1.SelectedIndex] : null;
}
protected void ListView1_Sorted(object sender, EventArgs e)
{
if (dk == null) return;
int i;
ListView1.DataBind();
for (i = 0; i < ListView1.DataKeys.Count; i++)
if(AreEqual(ListView1.DataKeys[i].Values,dk.Values)) break;
if (i >= ListView1.DataKeys.Count) return;
ListView1.SelectedIndex =i;
}
private bool AreEqual(System.Collections.Specialized.IOrderedDictionary x, System.Collections.Specialized.IOrderedDictionary y)
{
for (int i = 0; i < x.Count; i++)
if (!x[i].Equals(y[i])) return false;
return true;
}