Static & Dynamically adding GridColumns

3 posts, 0 answers
  1. Renzo Koning
    Renzo Koning avatar
    5 posts
    Member since:
    Feb 2010

    Posted 08 Mar 2010 Link to this post

    I have created a wrapper control around the telerik RadGrid to be able to set some default settings and implement some of my own methods. Within my wrapper user control I added the property ShowEditButton and ShowDeleteButton which implement my own update / delete action. Once this property is set I programmatically add a GridButtonColumn which represents the update and/or delete button in the grid.

    I add my custom action column with the following code:

    if (!IsPostBack){ 
    GridButtonColumn col = new GridButtonColumn(); 
    col.CommandName = col.Text = col.HeaderText = "Delete"
    col.UniqueName = "DeleteColumn"
    radDataGrid.Columns.Add(col); 



    If I execute this code within de Load event of my wrapper the column doesn't get added at all. If I execute the code in the Init event of my wrapper the column is added but with every (ajax) postback the column is being re-added to the grid which results in having multiple delete buttons within the grid.

    Implementation (Implementation of custom wrapper, DataGrid, see below):
    <myapp:DataGrid ID="grdAdminOverview" runat="server" ShowEditButton="true" ShowDeleteButton="true"
        <Columns> 
            <telerik:GridBoundColumn HeaderText="Firstname" DataField="firstname" /> 
            <telerik:GridBoundColumn HeaderText="Lastname" DataField="lastname" /> 
            <telerik:GridBoundColumn HeaderText="Username" DataField="username" /> 
            <telerik:GridBoundColumn HeaderText="Email" DataField="email" /> 
            <telerik:GridBoundColumn HeaderText="Isadmin" DataField="isadmin" /> 
        </Columns> 
    </myapp:DataGrid> 

    Wrapper (wraps RadGrid with default settings):
    <%@ Control Language="C#" AutoEventWireup="true" CodeBehind="DataGrid.ascx.cs" Inherits="Controls.DataGrid" %> 
    <telerik:RadGrid ID="radDataGrid" runat="server" AutoGenerateColumns="false" AllowPaging="true" Height="425px" Width="750px"
    </telerik:RadGrid> 


    As soon as I add my custom columns within the Init event of my custom control the columns are being added, but as said this results in having a column being added with each (ajax) postback. To prevent this from happening I tried checking if the column is already added (also in init event) using: radDataGrid.Columns.FindByUniqueNameSafe("DeleteColumn") but this always returns null.

    Am I doing something wrong here?
  2. robertw102
    robertw102 avatar
    265 posts
    Member since:
    Jul 2007

    Posted 08 Mar 2010 Link to this post

    Perhaps you should be adding those dynamic columns in the PreRender event of your wrapper control, since the RadGrid control should have created the columns before that event and thus allow you to add yours.

    I hope that helps.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Renzo Koning
    Renzo Koning avatar
    5 posts
    Member since:
    Feb 2010

    Posted 09 Mar 2010 Link to this post

    Hi,

    I tried doing it in the prerender event as you suggested but this results in the columns not being added at all. I have created a small test project with just the code to simulate my situation that I have here. As my previous explanation can be a little bit unclear.

    In the sample the custom columns are being added within DataGrid.ascx.cs in the "protected override void OnInit". When run you will see what I mean with the extra (empty) columns being added when the Update or Delete buttons are pressed in the Grid.


    Edit: I've also created a support ticket for this problem under my account with the test project zip attached to it.
Back to Top