Change Telerik RadGrid GridBoundColumn data field using c #

I have a RadGrid that is initialized in markup:

<telerik:RadGrid runat="server" ID="GridViewSelector" SkinID="GridViewSelectorSkin" AllowPaging="True" PageSize="12" AllowSorting="True" EnableViewState="true" 
    AllowCustomPaging="true" AllowFilteringByColumn="true" DataKeyNames="ID" AutoGenerateColumns="False" ShowFooter="false" style="float:left;"
    AllowMultiRowEdit="false" OnNeedDataSource="GridViewSelector_NeedDataSource"
    OnItemCommand="GridViewSelector_ItemCommand" EnableEmbeddedSkins="false" OnItemCreated="GridViewSelector_ItemCreated"
    OnSortCommand="GridViewSelector_SortCommand" 
    OnItemDataBound="GridViewSelector_ItemDataBound" 
    OnPreRender="GridViewSelector_PreRender" OnSelectedIndexChanged ="GridViewSelector_SelectedIndexChanged">
    <PagerStyle Visible="false" />
    <ClientSettings EnableRowHoverStyle="true">
        <Selecting AllowRowSelect="false" />
    </ClientSettings>
    <MasterTableView runat="server" AllowMultiColumnSorting="false" AllowNaturalSort="false" DataKeyNames="ID">
        <SortExpressions>
            <telerik:GridSortExpression FieldName="ExpenseDate" SortOrder="Ascending"  />
        </SortExpressions>
        <Columns>
            <telerik:GridButtonColumn ButtonType="ImageButton" ImageUrl="~/Images/ListingDown.png" CommandName="Select">                 
                <HeaderStyle HorizontalAlign="Center" Width="50px" />
                <ItemStyle HorizontalAlign="Center" Width="50px" />                    
            </telerik:GridButtonColumn>
            <telerik:GridBoundColumn HeaderText="Date" UniqueName="ExpenseDate"  DataField="ExpenseDate" SortExpression="ExpenseDate" HtmlEncode="false">
                <HeaderStyle HorizontalAlign="Center" Width="100px" />
                <ItemStyle HorizontalAlign="Center" Width="100px" />
            </telerik:GridBoundColumn>
            <telerik:GridBoundColumn HeaderText="Type" UniqueName="ExpenseType" DataField="Type" SortExpression="Type" HtmlEncode="false">
                <HeaderStyle HorizontalAlign="Center" Width="100px" />
                <ItemStyle HorizontalAlign="Center" Width="100px" />
            </telerik:GridBoundColumn>
            <telerik:GridBoundColumn HeaderText="Description" UniqueName="ExpenseDescription" DataField="Description" SortExpression="Description">
                <HeaderStyle HorizontalAlign="Center" />
            </telerik:GridBoundColumn>
            <telerik:GridBoundColumn HeaderText="Gross" UniqueName="Gross" DataField="Gross" SortExpression="Gross" HtmlEncode="false">
                <HeaderStyle HorizontalAlign="Center" Width="100px" />
                <ItemStyle HorizontalAlign="Center" Width="100px" />
            </telerik:GridBoundColumn>
            <telerik:GridBoundColumn HeaderText="" UniqueName="" DataField="" SortExpression="" HtmlEncode="">
                <HeaderStyle HorizontalAlign="Center" Width="100px" />
                <ItemStyle HorizontalAlign="Center" Width="100px" />
            </telerik:GridBoundColumn>               
        </Columns>
    </MasterTableView>
</telerik:RadGrid>

      

When the user clicks on the tab, I reformat the same RadGrid control in the .aspx.cs file. Here's an example:

                cols[1].HeaderText = "Claim No.";
                cols[1].DataType = typeof(System.Int64);
                ((GridBoundColumn)cols[1]).DataField = "ClaimNumber";


                cols[2].HeaderText = "Claimant";
                cols[2].DataType = typeof(System.String);
                ((GridBoundColumn)cols[2]).DataField = "Claimant";


                cols[3].HeaderText = "Date";
                cols[3].DataType = typeof(System.DateTime);
                ((GridBoundColumn)cols[3]).DataField = "ExpenseDate";
                ((GridBoundColumn)cols[3]).DataFormatString = "{0:" + HttpHelper.User.PreferredDateFormat + "}";

                cols[4].HeaderText = "Description";
                cols[4].DataType = typeof(System.String);
                ((GridBoundColumn)cols[4]).DataField = "ClaimNumber";


                cols[5].HeaderText = "Amount";
                cols[5].DataType = typeof(System.Decimal);
                ((GridBoundColumn)cols[5]).DataField = "Amount";
                ((GridBoundColumn)cols[5]).HeaderStyle.Width = 100;
                ((GridBoundColumn)cols[5]).ItemStyle.Width = 100;

      

This works pretty much except for one problem. The column that appears to be related to the sort is not populated with data. In particular, the top doesn't work:

((GridBoundColumn)cols[1]).DataField = "ClaimNumber";

      

Previously, it is filled with the date (dd / mm / yyyy) to which the sort expression is applied. All other fields (previously not having a sort expression) bounce and display the correct data.

Has anyone else experienced this?

+3


source to share





All Articles