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

Help with modifying hierarchical declaritive columns in code behind.

2 Answers 77 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Mike
Top achievements
Rank 1
Mike asked on 01 Mar 2012, 07:35 PM
Greetings Telerik Community,

I am currently and successfully using the code block below to turn an edit column into a delete column. I do this because my customer wants in-line add and delete but no edit. My issue now is I need to do this for a column that is nested 1 level deep in a hierarchical grid. The column name is "EditColumn2". The problem is this column doesn't appear to exist when the PreRender is called and throws an exception. My guess is it is created on the fly when the expand arrow is clicked. Which event can I use to add a similar code block to so I can apply the same logic to turn an editboundcolumn into a delete column for the nested table?

protected void GridPreRender(object sender, EventArgs e)
{
    foreach (GridDataItem item in MaintGrid.MasterTableView.Items)
    {
        ImageButton btn = (ImageButton)item["EditColumn"].Controls[0];
        btn.CommandName = "Delete";
        btn.ToolTip = "Delete";
        btn.ImageUrl = "~/Stylesheets/Images/Delete3.gif";
        btn.OnClientClick = "javascript:if(!confirm('Are you sure you wish to delete this record?')){return false;}";
    }
}

2 Answers, 1 is accepted

Sort by
0
Casey
Top achievements
Rank 1
answered on 01 Mar 2012, 08:53 PM
Hi Mike,

You could replace the column in the RadGrid with a GridTemplateColumn with an ImageButton in the ItemTemplate with these properties set there.  Is there a reason you are trying to do this in the code instead of by modifying the RadGrid in the .aspx page?

Here is how you could do this using the OnItemDataBound event of the RadGrid.

You'd need to add "OnItemDataBound="MaintGrid_ItemDataBound" to you RadGrid, as well as set the Name property of the MasterTableView and the DetailTableViews.

I hope this helps!
Casey

protected void MaintGrid_ItemDataBound(object sender, GridItemEventArgs e)
{
   if (e.Item is GridDataItem)
   {
       GridDataItem dataItm = e.Item as GridDataItem;
       if (e.Item.OwnerTableView.Name == "Master")
       {
            ImageButton btn = dataItm.FindControl("ImageButton1") as ImageButton;
            btn.CommandName = "Delete";
            btn.ToolTip = "Delete";
            btn.ImageUrl = "~/Stylesheets/Images/Delete3.gif";
            btn.OnClientClick = "javascript: if(!confirm('Are you sure you wish to delete this record?')){return false;}"; 
       }
       else if (e.Item.OwnerTableView.Name == "Detail")
       {
            ImageButton btn = dataItm.FindControl("ImageButton2") as ImageButton;
            btn.CommandName = "Delete";
            btn.ToolTip = "Delete";
            btn.ImageUrl = "~/Stylesheets/Images/Delete3.gif";
            btn.OnClientClick = "javascript: if(!confirm('Are you sure you wish to delete this record?')){return false;}"; 
       }
   }
}
0
Mike
Top achievements
Rank 1
answered on 01 Mar 2012, 10:22 PM
Thank you. That led me on the path to the solution. Only change I had to make was to declare the button the way I did in my original post instead of your way as it was causing a null exception for some reason.

To answer your question as to why I am using an edit column rather than a template column. This is because the edit column is the only way I could get the Add and Cancel buttons for the in-line add function to show the buttons. Having a delete column or a template column or any other columns without having an edit column made these two buttons not appear on the add command. I am happy I now have a working solution but if you know a way to add in the add/cancel buttons to the in-line add feature without an edit column I am intrigued.

Thank you again for your quick and excellent support.

Respectfully,

~Mike
Tags
Grid
Asked by
Mike
Top achievements
Rank 1
Answers by
Casey
Top achievements
Rank 1
Mike
Top achievements
Rank 1
Share this question
or