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

Set GridDropDownColumn value according to the value of another cell

1 Answer 116 Views
Grid
This is a migrated thread and some comments may be shown as answers.
io
Top achievements
Rank 1
io asked on 18 Feb 2020, 09:52 PM

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

1 Answer, 1 is accepted

Sort by
0
Attila Antal
Telerik team
answered on 21 Feb 2020, 02:43 PM

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.
Tags
Grid
Asked by
io
Top achievements
Rank 1
Answers by
Attila Antal
Telerik team
Share this question
or