I am having an issue on Sorting a ListBox with a dropdown. The dropdown will have 2 options:
Sort By #
Sort By Type
The ListBox contains a list of items which are initially ordered in ascending order by #. It has the following format:
- 1: Red
- 2: Green
- 3: Blue
- 4: Red
- 5: Red
If the user choses Sort By #, it should sort in descending order if it is already in ascending order and if in ascending order if it is in descending order. If the user choses Sort By Type, it should order the colors, then #, so the above list becomes:
- 1: Red
- 4: Red
- 5: Red
- 2: Green
- 3: Blue
The lisbox is gets its data from an ObjectDataSource which is defined as follows:
<asp:ObjectDataSource ID="odsColors" runat="server" SelectMethod="GetColors"
TypeName="XaiSoft.Data">
</asp:ObjectDataSource>
I have stubbed out a method for sorting, but this is where I am stuck at, I am not sure about how to do it:
public void SortColorList()
{
}
I updated the above method to show what I am going for, but I am getting the following error:
Both DataSource and DataSourceID are defined on 'lstColors'. Remove one definition.
protected void SortColorList()
{
XaiSoft.Data.Colors c = new XaiSoft.Data.Colors();
DataTable dt = c.GetColorList();
DataView dv = new DataView(dt);
dv.Sort = "[" + dv.Table.Columns["ColorName"].ColumnName + "] asc";
lstAuctions.DataSource = dv; //Error happens here.
lstAuctions.DataBind();
}
I got around the error, by setting the DataSourceId = string.empty;
Thanks for the help, XaiSoft