all groups > asp.net datagrid control > november 2006 >
You're in the

asp.net datagrid control

group:

Sorting with datasource ArrayList


Sorting with datasource ArrayList Martin Gregersen
11/10/2006 11:49:04 AM
asp.net datagrid control: Hi everybody

I have a GridView control where my datasource is an ArrayList, I would =
like to be able to sort the grid, therefore I set the AllowSorting to =
true, so far so good. The ArrayList consist of a collection of =
Customer-objects, where the "Title" (a string) is the name of the =
Customer.

Below is my sourcecode for the Sorting event and the control itself, I =
don't know what is wrong, but when I click a column to sort I get an: =
System.StackOverflowException was unhandled

Can anyone please show an example of a sorting that works with an =
ArrayList as datasource in a grid.

If you need any addiotnal details let me know. :o)

Thanks in advance

Martin Gregersen
WebShine A/S
gregersen(add)webshine(dot)dk

--- gwCustomerList_Sorting Source ----

protected void gwCustomerList_Sorting(object sender, =
GridViewSortEventArgs e)

{

Trace.Write("Customers.aspx.cs", "gwCustomerList_Sorting(object =
sender, GridViewSortEventArgs e)");



try

{

Trace.Write("SortExpression: " + =
e.SortExpression.ToString());

Trace.Write("SortDirection: " + e.SortDirection.ToString());

gwCustomerList.DataSource =3D _alCustomerList;

gwCustomerList.Sort(e.SortExpression, e.SortDirection);

gwCustomerList.DataBind();

}

catch (Exception ex)

{

Trace.Write("Exception: " + ex.Message);

}

}


---- Grid Source ----
<asp:GridView ID=3D"gwCustomerList" runat=3D"server" =
AutoGenerateColumns=3D"False"

CellPadding=3D"4" ForeColor=3D"#333333" GridLines=3D"None" =
OnPageIndexChanging=3D"gwCustomerList_PageIndexChanging" PageSize=3D"15" =
AllowPaging=3D"True" meta:resourcekey=3D"gwCustomerListResource1" =
AllowSorting=3D"True" OnSorting=3D"gwCustomerList_Sorting">

<FooterStyle BackColor=3D"#5D7B9D" Font-Bold=3D"True" =
ForeColor=3D"White" />

<Columns>

<asp:TemplateField SortExpression=3D"ElementNumber" =
HeaderText=3D"<%$ Resources:objCustomer, Number %>">

<ItemTemplate>

<%# =
link("ElementID",Eval("ElementId"),Eval("ElementNumber"))%>

</ItemTemplate>

</asp:TemplateField>

<asp:TemplateField HeaderText=3D"<%$ Resources:objCustomer, =
Phone %>" SortExpression=3D"Phone">

<ItemTemplate>

<%# (Eval("Address.Phone")) %>

</ItemTemplate>

</asp:TemplateField>

<asp:BoundField DataField=3D"Title" HeaderText=3D"<%$ =
Resources:objCustomer, Title %>" SortExpression=3D"Title" />

<asp:TemplateField HeaderText=3D"<%$ Resources:objCustomer, =
Attention %>">

<ItemTemplate>

<%# (Eval("Address.Contact")) %>

</ItemTemplate>

</asp:TemplateField>

<asp:TemplateField HeaderText=3D"<%$ Resources:objCustomer, =
Address %>">

<ItemTemplate>

<%# (Eval("Address.Address1")) %>

</ItemTemplate>

</asp:TemplateField> =20

</Columns>

<RowStyle BackColor=3D"#F7F6F3" ForeColor=3D"#333333" />

<EditRowStyle BackColor=3D"#999999" />

<SelectedRowStyle BackColor=3D"#E2DED6" Font-Bold=3D"True" =
ForeColor=3D"#333333" />

<PagerStyle BackColor=3D"#284775" ForeColor=3D"White" =
HorizontalAlign=3D"Center" />

<HeaderStyle BackColor=3D"#5D7B9D" Font-Bold=3D"True" =
ForeColor=3D"White" HorizontalAlign=3D"Left" />

<AlternatingRowStyle BackColor=3D"Beige" ForeColor=3D"#333333" =
/>

Re: Sorting with datasource ArrayList mtadd
11/10/2006 2:07:44 PM
The Sort method of the Gridview control raises the Sorting and Sorted
Events...that's why you're getting a stack overflow. Instead, you
should sort your data source (_alCustomerList) according to the
SortDirection and SortExpression provided by the EventArgs prior to
Databinding it to the Gridview in the Sorting Event.
AddThis Social Bookmark Button