This is a migrated thread and some comments may be shown as answers.

[Solved] Sorting RadGrid with a Dictionary<int, string> fails

1 Answer 172 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Julio Montelongo
Top achievements
Rank 1
Julio Montelongo asked on 01 Oct 2009, 09:34 PM
Hello Team, I have a problem handling the sorting of my RadGrid that has a generic Dictionary as a DataSource, I bind the data handling the NeedDataSource event and works fine for binding the data and also for auto paging, I have enabled AllowSorting but when I click the only column there is I get "Sys.WebForms.PageRequestManagerServerErrorException: Cannot find column Value." following I share my declarative binding and the datasource handling on the code behind, I hope you can send some light to solve this issue.

At the aspx:

            <telerik:RadGrid ID="gvwTrainingProgramsRad" runat="server" AutoGenerateColumns="false"  AllowPaging="true" AllowSorting="true" >
                <pagerstyle mode="NextPrevAndNumeric" position="Top"></pagerstyle>
                <mastertableview width="100%" DataKeyNames="Key" AllowSorting="true" >
                    <Columns>
                        <telerik:GridBoundColumn AllowSorting="true" DataType="System.String" UniqueName="ProgramName" SortExpression="Value" HeaderText="Program Name" DataField="Value" />
                    </Columns>
                </mastertableview>
            </telerik:RadGrid>

At the CodeBehind:

        void gvwTrainingProgramsRad_NeedDataSource(object source, GridNeedDataSourceEventArgs e)
        {
             gvwTrainingProgramsRad.DataSource = GetPrograms();
        }

The GetPrograms() method returns a Dictionary<int, string>, like I said it works well for binding but not for sorting, what am I doing wrong?


Thanks for your help


Full stacktrace:


[IndexOutOfRangeException: Cannot find column Value.]
   System.Data.DataTable.ParseSortString(String sortString) +523
   System.Data.DataView.CheckSort(String sort) +28
   System.Data.DataView.set_Sort(String value) +125
   Telerik.Web.UI.GridEnumerableFromDataView.PerformTransformation() +3662
   Telerik.Web.UI.GridEnumerableFromDataView.TransformEnumerable() +21
   Telerik.Web.UI.GridTableView.GetEnumerator(Boolean useDataSource, GridEnumerableBase resolvedDataSource, ArrayList dataKeysArray) +105
   Telerik.Web.UI.GridTableView.CreateControlHierarchy(Boolean useDataSource) +169
   Telerik.Web.UI.GridTableView.CreateChildControls(IEnumerable dataSource, Boolean useDataSource) +500
   System.Web.UI.WebControls.CompositeDataBoundControl.PerformDataBinding(IEnumerable data) +58
   System.Web.UI.WebControls.DataBoundControl.OnDataSourceViewSelectCallback(IEnumerable data) +114
   System.Web.UI.DataSourceView.Select(DataSourceSelectArguments arguments, DataSourceViewSelectCallback callback) +27
   System.Web.UI.WebControls.DataBoundControl.PerformSelect() +143
   Telerik.Web.UI.GridTableView.PerformSelect() +4
   System.Web.UI.WebControls.BaseDataBoundControl.DataBind() +71
   Telerik.Web.UI.GridTableView.DataBind() +238
   Telerik.Web.UI.GridSortCommandEventArgs.ExecuteCommand(Object source) +126
   Telerik.Web.UI.RadGrid.OnBubbleEvent(Object source, EventArgs e) +134
   System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +37
   Telerik.Web.UI.GridItem.OnBubbleEvent(Object source, EventArgs e) +38
   System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +37
   Telerik.Web.UI.GridItem.OnBubbleEvent(Object source, EventArgs e) +115
   System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +37
   System.Web.UI.WebControls.LinkButton.OnCommand(CommandEventArgs e) +118
   System.Web.UI.WebControls.LinkButton.RaisePostBackEvent(String eventArgument) +166
   System.Web.UI.WebControls.LinkButton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10
   System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
   System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +170
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +3437

 

1 Answer, 1 is accepted

Sort by
0
Julio Montelongo
Top achievements
Rank 1
answered on 02 Oct 2009, 09:51 PM
By the way I found the solution to the problem, I isolated the functionality on a small web app project on a single page and its codebehind, here is the handling of the needdatasource event for reproduction of the error:

        protected void gvwTrainingProgramsRad_OnNeedDataSource(object source, GridNeedDataSourceEventArgs e)
        {
            Dictionary<int, string> dic = new Dictionary<int, string>();
            dic.Add(1, "uno");
            dic.Add(2, "dos");
            dic.Add(3, "tres");

            gvwTrainingProgramsRad.DataSource = dic;
        }

aspx:
<body>
    <form id="form1" runat="server">
    <div>
    
        <asp:ScriptManager ID="ScriptManager1" runat="server">
        </asp:ScriptManager>
            <telerik:RadGrid ID="gvwTrainingProgramsRad" runat="server" AutoGenerateColumns="false"  AllowPaging="true" AllowSorting="true" AutoGenerateEditColumn="true" OnNeedDataSource="gvwTrainingProgramsRad_OnNeedDataSource" >
                <pagerstyle mode="NextPrevAndNumeric" position="Top"></pagerstyle>
                <mastertableview width="100%" DataKeyNames="Key" AllowSorting="true" >
                    <Columns>
                        <telerik:GridBoundColumn AllowSorting="true" DataType="System.String" UniqueName="ProgramName" SortExpression="Value" HeaderText="Program Name" DataField="Value" />
                    </Columns>
                </mastertableview>
            </telerik:RadGrid>
    </div>
    </form>
</body>

After testing, thinking and getting a bit frustrated I found that calling the ToArray method on the dictionary solved the issue:

I changed from:
gvwTrainingProgramsRad.DataSource = dic;
To:


gvwTrainingProgramsRad.DataSource = dic.ToArray<KeyValuePair<int, string>>();

I really don't understand the real difference because Dictionary also implements IEnumerable and ICollection but this made the trick, it works fine now, but if you don't transform the Dictionary to an IEnumerable it won't work, you can reproduce the error when clicking the column to sort, you will get the [IndexOutOfRangeException: Cannot find column Value.]





Tags
Grid
Asked by
Julio Montelongo
Top achievements
Rank 1
Answers by
Julio Montelongo
Top achievements
Rank 1
Share this question
or