使用C#更改Telerik RadGrid GridBoundColumn的数据字段

时间:2012-04-02 09:46:06

标签: c# asp.net .net web radgrid

我有一个在标记中初始化的RadGrid:

<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>

当用户单击选项卡时,我重新格式化.aspx.cs文件中的相同RadGrid控件。这是一个例子:

                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;

除了一个问题外,这几乎完美无缺。似乎已应用排序的列不会填充数据。特别是从上面来看,以下是行不通的:

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

之前它填充了日期(dd / mm / yyyy),其中应用了排序表达式。所有其他字段(之前没有排序表达式)都会反弹并显示正确的数据。

其他人经历过这个?

0 个答案:

没有答案