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

GridDropDownColumn question

1 Answer 29 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Kathy
Top achievements
Rank 1
Kathy asked on 18 Oct 2010, 12:06 AM
This is a 2 part question:
1) When a row is in edit mode, I want the dropdowncolumn to be read-only, the person can't change the selected item in the dropdown.
2) I want the datasource for the column to behave differently depending on if you are in edit mode or not.  For instance, my dropdown contains a list of categories.  Once you pick a category, it can't be used in other rows.  So if you had row1 and the category is fish, when the user adds a new record, the category dropdown would not have the fish entry in the list, just all the other categories.  So the user picks meat from the category drop down.  They save the row and try to add another row.  When they go to create this other row, the category drop down would not have fish and meat.  Of course, if you delete the row where you had selected fish, then fish would now be available in the category dropdown.  Any ideas on how to accomplish something like this?  Thanks.

1 Answer, 1 is accepted

Sort by
0
Radoslav
Telerik team
answered on 21 Oct 2010, 12:36 PM
Hello Kathy,

Regarding your first question:

You could find the DropDownList into the RadGrid.ItemDataBound event and disable it. For example:
void RadGrid2_ItemDataBound(object sender, GridItemEventArgs e)
{
   if (e.Item is GridEditFormItem && e.Item.IsInEditMode)
   {
      GridEditFormItem item = e.Item as GridEditFormItem;
      DropDownList ddl = item["GridDropDownColumn"].Controls[0] as DropDownList;
      ddl.Enabled = false;
   }
}

With respect to your second question:
You could keep into the Session the selected values from all rows:
public List<string> Items
{
    get
    {
        if (Session["Items"] == null)
        {
            Session["Items"] = new List<string>();
        }
 
        return Session["Items"] as List<string>;
    }
    set
    {
        Session["Items"] = value;
    }
}
 
void RadGrid2_ItemDataBound(object sender, GridItemEventArgs e)
{
    if (e.Item is GridDataItem)
    {
        GridDataItem gridItem = e.Item as GridDataItem;
        string text = gridItem["DDColumnUniqueName"].Text;
        if (!Items.Contains(text))
        {
            Items.Add(text);
        }
    }
}

When the user opens edit form you could check if items from the DropDownList are exist into the Items collection. If some item is contained into the Items you could remove it from the DropDownList:

void RadGrid2_ItemDataBound(object sender, GridItemEventArgs e)
    {
        if (e.Item is GridDataItem)
        {
            GridDataItem gridItem = e.Item as GridDataItem;
            string text = gridItem["DDColumnUniqueName"].Text;
            if (!Items.Contains(text))
            {
                Items.Add(text);
            }
        }
        if (e.Item is GridEditFormItem && e.Item.IsInEditMode)
        {
            DropDownList combo = item["DDColumnUniqueName"].Controls[0] as DropDownList;
            for(int i=0; i< combo.Items.Count;i++)
            {
                if (Items.Contains(combo.Items[i].Text))
                {
                    combo.Items.Remove(combo.Items[i]);
                    i--;
                }
            }
        }
    }

Additionally I am sending you a simple example which demonstrates the desire functionality. Please check it out and let me know if it helps you.

Greetings,
Radoslav
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
Tags
Grid
Asked by
Kathy
Top achievements
Rank 1
Answers by
Radoslav
Telerik team
Share this question
or