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

DataField is blank in RadGrid with dynamically defined columns

2 Answers 135 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Phil
Top achievements
Rank 1
Phil asked on 05 Oct 2014, 02:12 PM
I am defining a RadGrid in a user control with the following definition in the .ascx:

<telerik:RadGrid ID="grid1" OnNeedDataSource="grid1DS" PageSize="20" AllowPaging="True" AutoGenerateColumns="False"
    AllowSorting="True" CellSpacing="-1" GridLines="Both" Style="text-align: left;" OnSelectedIndexChanged="grid1_SelectedIndexChanged"
    AllowFilteringByColumn="True" EnableViewState="true" runat="server">
    <MasterTableView EnableColumnsViewState="true"></MasterTableView>
    <PagerStyle Mode="NextPrevAndNumeric" HorizontalAlign="Left" />
    <ClientSettings EnableRowHoverStyle="true" EnablePostBackOnRowClick="true">
        <Resizing AllowColumnResize="True" AllowRowResize="false" ResizeGridOnColumnResize="true"
            ClipCellContentOnResize="false" EnableRealTimeResize="true" AllowResizeToFit="true" />
        <Selecting AllowRowSelect="True" />
    </ClientSettings>
</telerik:RadGrid>

I am dynamically creating and adding each column with this:

Dim dscol As DataColumn
Dim col As Telerik.Web.UI.GridBoundColumn

...

col =
New GridBoundColumn()
col.DataField = dscol.ColumnName
col.UniqueName = dscol.ColumnName
col.HeaderText = dscol.ColumnName
col.Resizable = True
col.SortExpression = col.DataField
col.AllowFiltering = True
grid1.Columns.Add(col)

This creates a mostly working grid. The problem is that on the call to the grid1DS data source, a filter expression is returned with an empty expression for the DataField value (e.g. filtering on a name field for "Contains" John):

(it[""].ToString().Contains("John"))

Normally, it[""] would be it["DataField identifier"].  So apparently the DataField isn't being properly set in the browser, despite being properly defined in the code above (confirmed by breakpoints.) 

So - thoughts, solutions? 




2 Answers, 1 is accepted

Sort by
0
Phil
Top achievements
Rank 1
answered on 05 Oct 2014, 02:59 PM
I can answer my own question. With some further research I ran across this 2008 thread:

http://www.telerik.com/forums/radgrid-runtime-columns

The problem essentially lies in the ordering of operations for the creation of the GridBoundColumn. The newly created column MUST be added FIRST (in example above, grid1.Columns.Add(col)) BEFORE any column properties are set.

As was noted 6 years ago, it would be fair to call that a bug on Telerik's part, and a really hard to find, aggravating, and unnecessary problem.
0
Konstantin Dikov
Telerik team
answered on 08 Oct 2014, 03:56 PM
Hello Phil,

Thank you for contacting us.

I am glad to see that you were able to resolve the issue.

Although you have found the root of the problem, I want to point your attention to our help article for programmatically created RadGrid, where information regarding the order of adding a column and settings its properties is available for both approaches - in the Page's Load and Init events:
I also want to point out that this is not a bug in our controls, but is a ASP.NET AJAX limitation with the Page's Init event handler and attaching properties to controls. You can take a look at the following forum thread regarding this limitation:
Please let us know if any further questions arise on this matter.


Regards,
Konstantin Dikov
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
Tags
Grid
Asked by
Phil
Top achievements
Rank 1
Answers by
Phil
Top achievements
Rank 1
Konstantin Dikov
Telerik team
Share this question
or