Search code examples
c#gridviewdrop-down-menuedititemtemplate

Access DropDownList in EditItemTemplate within GridView on GridView_RowEditing


I inserted a DropDownList into an EditItemTemplate within a GridView and I'd like to fill this DropDownList with Items based on a Value in another GridView-Column on the same row.

Let me give you an example:

If the row looks like this:

Itemname, string, TextBox, (editbutton)

I want the click on EditButton to transform the string "TextBox" into a DropDownList with the Options "TextBox" or "DropDownList", but if the row looks like this:

Itemname, bool, CheckBox, (editbutton)

I want the DropDown to only feature "CheckBox" and "RadioButton".

So, logically, I need to access a) the Column containing the value of the datatype-field (string or bool) and b) access the DropDown-List to modify its items within the GridView_RowEditing-Event.

I already managed doing a), can anyone help me with b)?

My Code currently looks like this:

        protected void gridVariables_RowEditing(object sender, GridViewEditEventArgs e)
    {
        switch (gridVariables.Rows[e.NewEditIndex].Cells[2].Text)
        {
            case "string":
            case "double":
            case "long":
                break;
            case "bool":
                break;
            default:
                break;
        }
        gridVariables.EditIndex = e.NewEditIndex;
        gridVariables_DataBind();
    }

Thanks,

Dennis


Solution

  • Since you did not post the gridview code ill just do this in psuedo code

    In your rowdatabound Event handler
    Check if the row is the edit row
    ddl.Items.Clear()
    if (dataitem is textbox or ddl)
       ddl.items.add("textBox");
       ddl.items.add("DDL");
    else
       ddl.items.add("CB");
       ddl.items.add("RB");