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

RadGrid Dynamic columns and ItemDataBound

1 Answer 490 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Pat Brophy
Top achievements
Rank 1
Pat Brophy asked on 23 Jun 2011, 08:33 PM
Can you statically define a column in a radgrid, dynamically add another column, and then manipulate either column in the grid's ItemDataBound event?

First I statically defined a RadGrid such as the following:

<telerik:RadGrid ID="grdResults" Height="100%" runat="server" OnNeedDataSource="grdResults_NeedDataSource" OnItemDataBound="grdResults_ItemDataBound" GridLines="None" AllowSorting="true" AllowPaging="false" AutoGenerateColumns="false">
    <ClientSettings>
        <Scrolling AllowScroll="true" UseStaticHeaders="true" SaveScrollPosition="false" />
        <Selecting AllowRowSelect="true" />
    </ClientSettings>
    <MasterTableView>
        <Columns>             
          
<telerik:GridBoundColumn HeaderText="StatusCodeDescription" UniqueName="StatusCodeDescription" DataField="StatusCodeDescription" HeaderStyle-Width="8%"></telerik:GridBoundColumn>
  
        </Columns>
    </MasterTableView>
</telerik:RadGrid>


then dynamically add a column in the page load like:

if (!Page.IsPostBack)
{
    var boundColumn = new GridBoundColumn();
    e.GridColumns.Add(boundColumn);
 
    boundColumn.HeaderText = "Select";
    boundColumn.DataField = "Id";
    boundColumn.UniqueName = "selectId";
}

and still use the ItemDataBound event to manipulate the dynamically added column?  
I can retrieve and manipulate the statically defined column, but I am unable to do the same for the 
dynamically added column?  

What am I doing wrong?


private void GridItemDataBound(object sender, GridItemEventArgs e)
{
    if (e.Item is GridDataItem)
    {
        var item = (GridDataItem)e.Item;
        item["selectId"].Text = "test";
 
     if (item["StatusCodeDescription"].Text.Length > 5)
            item["StatusCodeDescription"].Text = item["StatusCodeDescription"].Text.Substring(0, 2) + "...";
   }
}

1 Answer, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 24 Jun 2011, 05:20 AM
Hello Pat Brophy

RadGrid does not support mixing declarative grid columns with grid columns added dynamically at runtime.You should either create all the columns in the grid programmatically,or else define them all in the ASPX file.
For more information check Programmatic creation.

Thanks
Shinu
Tags
Grid
Asked by
Pat Brophy
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Share this question
or