DataGridView Sorting

DataGridView Sorting

Old forum URL: forums.lhotka.net/forums/t/2927.aspx


JonM posted on Tuesday, May 22, 2007

Most of my CSLA experience was with CSLA 1.52 in .NET 2.0.  I was able to sort data from my business collections in the datagridview without any problem.  How do I do that with CSLA 2.0?  Basically the grids don't allow me to click on the column header, it makes me think there is something else I need to do.

ajj3085 replied on Tuesday, May 22, 2007

Are you wrappnig the list in a SortedBindingList?

david.wendelken replied on Wednesday, May 23, 2007

I've been really happy using the ObjectListView class that's part of the CslaContrib project. 

dcleven replied on Wednesday, May 23, 2007

2 steps are required.

1. Enable sorting on the grid. right click / Edit Columns on the grid, pick the column and change SortMode to "Automatic".

2. Provide a sorted binding source. Like this:

MyBusinessObjectList list = MyBusinessObjectList.GetList();

Csla.SortedBindingList<BusinessObjectInList> sortedList = new Csla.SortedBindingList<BusinessObjectInList>(list);

sortedList.ApplySort("PropertyToSortOn",  listSortDirection.Ascending);

myBindingSource.DataSource = sortedList;

JonM replied on Thursday, May 24, 2007

Ah hah!  I needed to set the column's sortmode to automatic.  Now it works just like I want.  Thanks!

av_harris replied on Saturday, June 09, 2007

Hi,

I also have problems sorting. I looked for the SortMode property but it isn'y available in the GUI nor in code. Is this a property for WinForm only? I have my grid on a web form and the sort isn't getting applied correctly.

Dim list As ContactList = ContactList.GetContactList
Dim sorted As New SortedBindingList(Of ContactInfo)(list)

sorted.ApplySort("fname", ListSortDirection.Ascending)
' return sorted result

e.BusinessObject = sorted

Am I missing something here?

6/13/06 - revelation

FYI, column names are case sensitive AS DEFINED in the BO, not in the table. Should have known this but learned the hard way.

Once implemented, I also learned that since I really need to sort on 2 columns, this isn't going to be the best method!

Brian Criswell replied on Sunday, June 10, 2007

It will not solve any UI issues, but the ObjectListView allows you to sort on two properties.
Dim sorted as new ObjectListView(list, "FName, LName")

mangesh replied on Thursday, January 10, 2008

hi,

I am trying to bind a namevalue pair from a namevaluelist base to gridview and binding event is calling rowdatabound event but unable to find the name value class there please help.

 

Server Error in '/' Application.

Unable to cast object of type 'NameValuePair[System.Guid,System.String]' to type 'Csla.NameValueListBase`2[System.Guid,System.String]'.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.InvalidCastException: Unable to cast object of type 'NameValuePair[System.Guid,System.String]' to type 'Csla.NameValueListBase`2[System.Guid,System.String]'.

Source Error:

Line 75:             if (e.Row.RowType == DataControlRowType.DataRow)
Line 76:             {
Line 77:                 NameValueListBase<Guid, string> drview = (NameValueListBase<Guid,  string>)e.Row.DataItem;
Line 78:                 //NameValueConfigurationCollection drview = (NameValueConfigurationCollection)e.Row.DataItem;
Line 79:                 CheckBox chkSelected = new CheckBox();

Source File: D:\UTCWebApp\UTCWebApp\Default.aspx.cs    Line: 77

Stack Trace:

[InvalidCastException: Unable to cast object of type 'NameValuePair[System.Guid,System.String]' to type 'Csla.NameValueListBase`2[System.Guid,System.String]'.]
   UTCWebApp._Default.gvPrivilegeList_RowDataBound(Object sender, GridViewRowEventArgs e) in D:\UTCWebApp\UTCWebApp\Default.aspx.cs:77
   System.Web.UI.WebControls.GridView.OnRowDataBound(GridViewRowEventArgs e) +96
   System.Web.UI.WebControls.GridView.CreateRow(Int32 rowIndex, Int32 dataSourceIndex, DataControlRowType rowType, DataControlRowState rowState, Boolean dataBind, Object dataItem, DataControlField[] fields, TableRowCollection rows, PagedDataSource pagedDataSource) +211
   System.Web.UI.WebControls.GridView.CreateChildControls(IEnumerable dataSource, Boolean dataBinding) +4310
   System.Web.UI.WebControls.CompositeDataBoundControl.PerformDataBinding(IEnumerable data) +89
   System.Web.UI.WebControls.GridView.PerformDataBinding(IEnumerable data) +38
   System.Web.UI.WebControls.DataBoundControl.OnDataSourceViewSelectCallback(IEnumerable data) +126
   System.Web.UI.DataSourceView.Select(DataSourceSelectArguments arguments, DataSourceViewSelectCallback callback) +98
   System.Web.UI.WebControls.DataBoundControl.PerformSelect() +153
   System.Web.UI.WebControls.BaseDataBoundControl.DataBind() +99
   System.Web.UI.WebControls.GridView.DataBind() +23
   UTCWebApp._Default.FillRoleList() in D:\UTCWebApp\UTCWebApp\Default.aspx.cs:56
   UTCWebApp._Default.Page_Load(Object sender, EventArgs e) in D:\UTCWebApp\UTCWebApp\Default.aspx.cs:24
   System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +31
   System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +68
   System.Web.UI.Control.OnLoad(EventArgs e) +88
   System.Web.UI.Control.LoadRecursive() +74
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +3036


Version Information: Microsoft .NET Framework Version:2.0.50727.832; ASP.NET Version:2.0.50727.832

 

ajj3085 replied on Thursday, January 10, 2008

Well, your problem is right here:

NameValueListBase<Guid, string> drview = (NameValueListBase<Guid,  string>)e.Row.DataItem;

The row's DataItem will be of NameValuePair[System.Guid,System.String], not NameValueListBase.

HTH
Andy

Copyright (c) Marimer LLC