OnEditCommand - how to access column values and manipulate UI

4 posts, 0 answers
  1. matt
    matt avatar
    259 posts
    Member since:
    Oct 2007

    Posted 29 Jan 2013 Link to this post

    hello,

    in our RadGrid we use in-row editing. however, i need to make certain editable elements read-only (or disabled), depending on the value in another bound column. for example, if the value of the "Status" read-only GridBoundColumn is "Rejected", then i need to disable editing for the other editable GridCheckBoxColumn and GridDropDownColumn columns we have. 

    i think this may be done in the OnEditCommand handler...but I'm not sure what the syntax is. have checked the docs but didn't see it. 

    i determined i can use the item.Cells collection to get the Status column's string value, like so:

    protected void gridItems_EditCommand(object source, GridCommandEventArgs e)
    {
        GridEditableItem editableItem = e.Item as GridEditableItem;
     
        string status = e.Item.Cells[6].Text; //i mapped this out, 6 is the Status column value.
     
        switch (status.ToLower())
        {
            case "rejected":
                //TODO: disable the UI for editing my particular columns here:
     
                // GridCheckBoxColumn
                // GridDropDownColumn
                // GridTemplateColumn
     
                break;
        }
    }


    ...or do i intercept this on the OnCreateColumnEditor="gridItems_CreateColumnEditor" or OnItemDataBound="gridItems_ItemDataBound" events/handlers?

    thanks,
    matt
  2. matt
    matt avatar
    259 posts
    Member since:
    Oct 2007

    Posted 29 Jan 2013 Link to this post

    ok, if found my solution -- it was not in the OnEditCommand event handler, but rather the ItemDataBound's. like so:

    protected void gridItems_ItemDataBound(object source, GridItemEventArgs e)
    {
        if (e.Item.IsInEditMode && e.Item is GridEditableItem)
        {
            //NOTE: if this grid supported inserts, we'd have to test against "e.Item.ItemIndex == -1", since -1 is used by inserts.
     
            GridEditableItem editableItem = e.Item as GridEditableItem;
     
            //get test status status
            string status = ((TextBox)editableItem["Status"].Controls[0]).Text;
     
            switch (status.ToLower())
            {
                case "rejected":
                    editableItem["UseInAllocation"].Enabled = false;
                    editableItem["UseInToleranceCalculation"].Enabled = false;
                    editableItem["AcceptanceStatus"].Enabled = false;                      
                    break;
            }
        }
    }


    ...this does the trick!

    and heres how to disable/remove the "Update" command GridLinkButton:

    http://www.telerik.com/community/forums/aspnet-ajax/grid/itemdatabound---conditionally-disable-command-link-button.aspx
  3. Matt
    Matt avatar
    57 posts
    Member since:
    Jun 2012

    Posted 27 Nov Link to this post

    I know this post is old but I'm stuck regardless...

    This isn't being called but is in the mark-up:

    OnItemDataBound="RadGrid1_ItemDataBound"

    And here is the method:

     protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
            {
                if(e.Item is GridEditableItem && e.Item.IsInEditMode && e.Item.ItemIndex > -1)
                {
                    GridEditableItem gridEditableItem = (GridEditableItem)e.Item;
                    ((RadDropDownList)gridEditableItem["UserRoleDesc"].Controls[0]).SelectedValue = gridEditableItem["Wbg_ID"].Text;

                    RadDropDownList RDDL_Wbg_ID = (RadDropDownList)gridEditableItem.FindControl("RDDL_Wbg_ID");
                    RDDL_Wbg_ID.SelectedValue = gridEditableItem["Wbg_ID"].Text;
                    RadDropDownList RDDL_AccessLevel = (RadDropDownList)gridEditableItem.FindControl("RDDL_AccessLevel");
                    RDDL_AccessLevel.SelectedValue = gridEditableItem["AccessLevel"].Text;
                    RadDropDownList RDDL_ExemptStat = (RadDropDownList)gridEditableItem.FindControl("RDDL_ExemptStat");
                    RDDL_ExemptStat.SelectedValue = gridEditableItem["ExemptStat"].Text;
                }
            }

    Neither of these methods are working. Even if I put a RadAlert in, nothing happens. I also have AllowMultiRowEdit="true", if that makes a difference.

  4. Marin Bratanov
    Admin
    Marin Bratanov avatar
    3916 posts

    Posted 30 Nov Link to this post

    Hi Matt,

    Can you confirm that AutoEventWireup="true" in the Page directive?

    You can also try attaching the handler in the code behind:

    protected void Page_Init(object sender, EventArgs e)
    {
        RadGrid1.ItemDataBound += RadGrid1_ItemDataBound;
    }
     
    private void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
    {
        //code here
    }

    The only other reason I can think of is that the grid is bound on the client and so its server events would not fire. 

    If neither helps, I suggest you open a ticket and send us a small runnable example that showcases the scenario and the problem.

    Regards,

    Marin Bratanov
    Progress Telerik
    Try our brand new, jQuery-free Angular components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
Back to Top