Set GridDropDownColumn value according to the value of another cell

2 posts, 0 answers
  1. io
    io avatar
    6 posts
    Member since:
    Jan 2020

    Posted 18 Feb Link to this post

    Hello

     

    I have a radgrid with a GridDropDown that is only displayed when the radgrid is in edit mode.

    What I want to accomplish is to set the initial value of the DropDown based on the value of another cell, and still be able to open the dropbox for my edit method

    For example:

    IdEstatus || Estatus

    1              || Pending

    2              || Delivered

     

    <telerik:GridBoundColumn DataField="IdEstatus" HeaderText="<%$ Resources:Language, lbl_rg_estatus %>" UniqueName="IdEstatus" Display="false" ReadOnly="true" AllowFiltering="false" AutoPostBackOnFilter="true" CurrentFilterFunction="contains" ShowFilterIcon="false" FilterControlWidth="100%" HeaderStyle-HorizontalAlign="Center"></telerik:GridBoundColumn>
    <telerik:GridDropDownColumn UniqueName="Estatus"  ListTextField="Descripcion" ListValueField="IdEstatus" Display="false" AllowFiltering="false" HeaderText="<%$ Resources:Language, lbl_rg_estatus %>" ItemStyle-CssClass="form-control form-control-sm"></telerik:GridDropDownColumn>

     

    protected void rgDetalles_ItemDataBound(object sender, GridItemEventArgs e)
            {
                if (e.Item is GridEditableItem && e.Item.IsInEditMode)
                {
                    GridDropDownColumnEditor editor = editMan.GetColumnEditor("Estatus") as GridDropDownColumnEditor;
                    editor.DataSource = CDatos.SP_PeticionesAlmacen.SP_ObtenerEstatus();
                    editor.DataBind();
                }

     

    Database

    Estatus

    IdEstatus || Description

    1              || Pending

    2              || Delivered

    3              || Canceled

     

    The initial value of the combobox should be based according to the value in the cell "IdEstatus", but still be able to open and show the rest of the options

    Thanks

  2. Attila Antal
    Admin
    Attila Antal avatar
    571 posts

    Posted 21 Feb Link to this post

    Hi,

    You are missing a reference to the editMan variable inside the ItemDataBound event handler. It must be declared every time the ItemDataBound event fires, see Accessing the column editors programmatically

    protected void rgDetalles_ItemDataBound(object sender, GridItemEventArgs e)
    {
        if (e.Item is GridEditableItem && e.Item.IsInEditMode)
        {
            var editableItem = (GridEditableItem)e.Item;
    
            var editMan = editableItem.EditManager; // <-- This is a must
    
            GridDropDownColumnEditor editor = editMan.GetColumnEditor("Estatus") as GridDropDownColumnEditor;
        }
    }

     

    Examples, instructions for Accessing values of the Cells are described in the Accessing Values and Controls article.

    That would look like this.

    protected void rgDetalles_ItemDataBound(object sender, GridItemEventArgs e)
    {
        if (e.Item is GridEditableItem && e.Item.IsInEditMode)
        {
            var editableItem = (GridEditableItem)e.Item;
    
            string dataKeyValue = editableItem.GetDataKeyValue("ID").ToString();
            string cellText = editableItem["CustomerName"].Text;
        }
    }

     

    The Editor is like a DropDownList. It has properties such as SelectedText, SelectedValues. These properties will determine which items to be Pre-Selected in the editor, either by Text or by Value.

    GridDropDownColumnEditor editor = editMan.GetColumnEditor("Estatus") as GridDropDownColumnEditor;
    
    string CustomerID = editableItem.GetDataKeyValue("CustomerID").ToString();
    string AnotherColumnsValue = editableItem.GetDataKeyValue("AnotherColumnsValue").ToString();
    
    editor.DataSource = CDatos.SP_PeticionesAlmacen.SP_ObtenerEstatus();
    editor.DataBind();
    
    // Set the Selected Text
    editor.SelectedText = AnotherColumnsValue;
    
    // OR
    
    // set the Selected Value
    editor.SelectedValue = CustomerID;

     

    Kind regards,
    Attila Antal
    Progress Telerik

    Get quickly onboarded and successful with UI for ASP.NET AJAX with the Virtual Classroom technical trainings, available to all active customers. Learn More.
Back to Top