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

Changes made to in ItemCreated event not persisting

2 Answers 61 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Nate
Top achievements
Rank 1
Nate asked on 25 Aug 2008, 10:42 PM
Hey everyone, I have a gridbuttoncolumn at the top level of a 3 level hierarchical grid and I am trying to modify the cells of this column in the ItemCreated event for the grid.  It works fine on initial load but then when a column is expanded it loses the changes.  If there is a better way of doing this please let me know but really I just wan't the link to display if a condition is true that I am checking in the ItemCreated event.  The code for the event is below.


protected void grdVariableLst_ItemCreated(object sender, GridItemEventArgs e)  
        {  
            if (e.Item.OwnerTableView.Name == "Variables")  
            {  
                if (e.Item.ItemType == GridItemType.Item || e.Item.ItemType == GridItemType.AlternatingItem)  
                {  
                    GridDataItem item;  
                    item = (GridDataItem)e.Item;  
                    if (int.Parse(((System.Data.DataRowView)(e.Item.DataItem)).Row.ItemArray[4].ToString()) == 1)  
                    {  
                        item["DeleteColumn"].Text = "Delete";  
                    }  
                }  
            }  
        } 

2 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 26 Aug 2008, 06:46 AM
Hi Tim,

Try changing the Text in the PreRender event and see if it is working?

CS:
 protected void RadGrid1_PreRender(object sender, EventArgs e) 
    { 
        foreach (GridDataItem item in RadGrid1.Items) 
        { 
            if (item.OwnerTableView.Name == "Master") 
            { 
               //You can implement your custom logic
                string strtxt = item["ProductName"].Text.ToString(); 
                if (strtxt == "Chai") 
                { 
                    item["DeleteColumn"].Text = "Delete"
                } 
            } 
        } 
    } 


Thanks
Shinu
0
Nate
Top achievements
Rank 1
answered on 26 Aug 2008, 03:17 PM
Hi Shinu, thanks for responding.  I actually played around with it last night and found something that worked.  If you modify the controls inside the cells as opposed to just modifying the text property of the cell the changes are persisted to viewstate.  I also changed from using to text to images and it looks really nice.  Here is the code.

if (e.Item.OwnerTableView.Name == "Variables")  
            {  
 
                if (e.Item.ItemType == GridItemType.Item || e.Item.ItemType == GridItemType.AlternatingItem)  
                {  
                    GridDataItem item;  
                    item = (GridDataItem)e.Item;  
                    if (int.Parse(((System.Data.DataRowView)(e.Item.DataItem)).Row.ItemArray[4].ToString()) == 1)  
                    {  
                        (item["DeleteColumn"].Controls[0] as ImageButton).ImageUrl = "../RadControls/Grid/Skins/Default/NotSelectedMenu.gif";  
                    }  
                    else  
                    {  
                        (item["DeleteColumn"].Controls[0] as ImageButton).ImageUrl = "../RadControls/Grid/Skins/Default/Delete.gif";  
                          
                    }  
                }  
            } 
Tags
Grid
Asked by
Nate
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Nate
Top achievements
Rank 1
Share this question
or