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

error on init sortExpression in code behind

1 Answer 158 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Murray
Top achievements
Rank 1
Murray asked on 26 Mar 2010, 11:20 PM
I tried the following in my Page_Load event based on search in forum:
protected void Page_Load(object sender, EventArgs e)
{
    ...
    // make call to get data
    List<QMarkRow> qmarkRows = qmd.getData(
        sessFilter,
        sessSort,
        pageSize);

    // set datasource
    grdList.DataSource = qmarkRows;
    grdList.VirtualItemCount = qmd.Count;
    grdList.PageSize = pageSize;
    grdList.CurrentPageIndex = qmd.PageIndex;
    grdList.DataBind();
    // tried below in GrdList_PreRender event still error
    if (sessSort != null && sessSort.Length > 0)
    {
        // create GridSortExpression and add to grid
        // format of sessSort: Sort<Coln> <ASC|DESC>
        GridSortExpression gse = new GridSortExpression();
        int index = sessSort.IndexOf(" ");
        gse.FieldName = sessSort.Substring(4, index - 4);
        gse.SortOrder = sessSort.Substring(index + 1).Equals("ASC") ? GridSortOrder.Ascending : GridSortOrder.Descending;
        grdList.MasterTableView.SortExpressions.Add(gse);
        grdList.MasterTableView.Rebind();
    }
...
}
I get error on Rebind() call:
IndexOutOfRangeException was unhandled by user code
cannot find column Col1

Col1 is defined in grid:
<telerik:RadGrid
        ID="grdList" runat="server"
        EnableViewState="false"
        Height="328px"
        AllowPaging="true"
        AllowCustomPaging="true"
        AllowSorting="true"
        AllowFilteringByColumn="true"
        AutoGenerateColumns="False"
        PageSize="12"
        Skin="WebBlue" OnPreRender="GrdList_PreRender">
    <MasterTableView
            ClientDataKeyNames="Col1,Col2,Col3,Col4,Col5,Col6,Col7,Col8,Col9,Col10"
            EnableViewState="False">
        <Columns>
            <telerik:GridBoundColumn DataField="Col1" UniqueName="Col1"
                DataType="System.String"
                HeaderText="COL1" Visible="false" DataFormatString="<nobr>{0}</nobr>" >
                <HeaderStyle Width="80px" HorizontalAlign="left"  />
                <ItemStyle Width="80px" HorizontalAlign="left" />
            </telerik:GridBoundColumn>                        
...

This grid is defined in user control and everything else works except setting sortExpression.

1 Answer, 1 is accepted

Sort by
0
Pavlina
Telerik team
answered on 31 Mar 2010, 11:54 AM
Hi Murray,

I went through your code and noticed that you use simple binding with DataBind() calls. In order to avoid this problem, please consider switching to advanced binding with NeedDataSource even handling or assign a data source control for the grid and let me know whether this addresses the issue.

Regards,
Pavlina
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
Tags
Grid
Asked by
Murray
Top achievements
Rank 1
Answers by
Pavlina
Telerik team
Share this question
or