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

Add a button to a cell depending on another column

1 Answer 71 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Karl B
Top achievements
Rank 1
Karl B asked on 09 Oct 2014, 03:24 PM
I need to have a button appear in a cell depending on another column. How can I accomplish this?

For example in the Cascading Combobox Example I would like to see a button appear in Food for rows where Vegetables are selected and I would like the dropdown cell to appear when Fruits are selected.

FYI I am wanting this button to launch a complex dialog that will return data to the cell when closed.

1 Answer, 1 is accepted

Sort by
0
Accepted
Dess | Tech Support Engineer, Principal
Telerik team
answered on 14 Oct 2014, 08:52 AM
Hello Karl,

Thank you for writing.

In order to display a button in the "Food" cell when the "FoodType" is Vegetable, it is appropriate to use a custom editor, derived from the RadTextBoxEditor. You can add a button inside the RadTextBoxEditorElement on which Click event to launch the custom form. Here is a sample implementation:
public class CustomRadTextBoxEditor : RadTextBoxEditor
{
    public CustomRadTextBoxEditor()
    {
        RadButtonElement button = new RadButtonElement();
        button.Click += new EventHandler(button_Click);
        button.Padding = new Padding(2, 0, 2, -2);
        button.Margin = new Padding(0, 0, 0, 0);
        button.Text = "...";
 
        StackLayoutElement stackPanel = new StackLayoutElement();
        stackPanel.Orientation = Orientation.Horizontal;
        stackPanel.Margin = new Padding(1, 0, 1, 0);
        stackPanel.Children.Add(button);
 
        RadTextBoxEditorElement tbElement = this.EditorElement as RadTextBoxEditorElement;
        RadTextBoxItem tbItem = tbElement.TextBoxItem;
        tbElement.Children.Remove(tbItem);
 
        DockLayoutPanel dockPanel = new DockLayoutPanel();
        dockPanel.Children.Add(stackPanel);
        dockPanel.Children.Add(tbItem);
        DockLayoutPanel.SetDock(tbItem, Telerik.WinControls.Layouts.Dock.Left);
        DockLayoutPanel.SetDock(stackPanel, Telerik.WinControls.Layouts.Dock.Right);
        tbElement.Children.Add(dockPanel);
    }
 
    private void button_Click(object sender, EventArgs e)
    {
        RadMessageBox.Show("Open your custom dialog");
    }
}

private void radGridView1_CellEditorInitialized(object sender, GridViewCellEventArgs e)
{
    if (e.Column.HeaderText == "Food")
    {
        if (this.radGridView1.CurrentRow.Cells["FoodType"].Value != DBNull.Value &&
            this.radGridView1.CurrentRow.Cells["FoodType"].Value != null)
        {
            RadDropDownListEditor editor = this.radGridView1.ActiveEditor as RadDropDownListEditor;
            if (editor != null)
            {
                RadDropDownListEditorElement editorElement = (RadDropDownListEditorElement)editor.EditorElement;
                if (int.Parse(this.radGridView1.CurrentRow.Cells["FoodType"].Value.ToString()) == 0)
                {
                    editorElement.DataSource = vegetablesList;
                }
                else
                {
                    editorElement.DataSource = fruitsList;
                }
                editorElement.SelectedValue = null;
                editorElement.SelectedValue = this.radGridView1.CurrentCell.Value;
            }
        }
    }
}
 
private void radGridView1_CellValueChanged(object sender, GridViewCellEventArgs e)
{
    if (e.Column.HeaderText == "FoodType")
    {
        e.Row.Cells["Food"].Value = null;
    }
}
 
private void radGridView1_EditorRequired(object sender, EditorRequiredEventArgs e)
{
    if (this.radGridView1.CurrentColumn.HeaderText == "Food" &&
        (int)this.radGridView1.CurrentRow.Cells["FoodType"].Value == 0)
    {
        e.EditorType = typeof(CustomRadTextBoxEditor);
    }
}

Note that this is just a sample approach and it may not cover all possible cases. Feel free to modify it on a way which suits your requirement best. Our GridView >> Using custom editors help article is quite useful on this topic.

I would like to note that for the rest of the "Food" cells, e.g. "FoodType" is Fruit, the default RadDropDownListEditor will appear.

I hope this information helps. Should you have further questions, I would be glad to help.

Regards,
Desislava
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
Tags
GridView
Asked by
Karl B
Top achievements
Rank 1
Answers by
Dess | Tech Support Engineer, Principal
Telerik team
Share this question
or