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

Static & Dynamically adding GridColumns

2 Answers 67 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Renzo Koning
Top achievements
Rank 1
Renzo Koning asked on 08 Mar 2010, 04:35 PM
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 Answers, 1 is accepted

Sort by
0
robertw102
Top achievements
Rank 1
answered on 08 Mar 2010, 04:55 PM
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.
0
Renzo Koning
Top achievements
Rank 1
answered on 09 Mar 2010, 10:18 AM
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.
Tags
Grid
Asked by
Renzo Koning
Top achievements
Rank 1
Answers by
robertw102
Top achievements
Rank 1
Renzo Koning
Top achievements
Rank 1
Share this question
or