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

add button to GridViewComboBoxColumn

5 Answers 158 Views
GridView
This is a migrated thread and some comments may be shown as answers.
John
Top achievements
Rank 1
John asked on 28 Dec 2012, 06:53 PM
I need to add a button next to the drop down button in a GridViewComboBoxColumn.  is that possible?  the user want's a button displayed when in the cell next to the drop down to add new items to the drop down for the grid.  i've seen stuff where it was customized to add new stuff typed in the GridViewComboBoxColumn but they don't want to do that.  they want a specific click step and a popup to add it.

Thanks!

5 Answers, 1 is accepted

Sort by
0
Accepted
Peter
Telerik team
answered on 03 Jan 2013, 07:38 AM
Hello John,

Thank you for writing.

You should handle RadGridView's CellEditorInitialized event to add a button to the DropDownElement's children collection. Please, refer to the code sniped below:
public Form1()
{
    InitializeComponent();
    this.radGridView1.CellEditorInitialized += new GridViewCellEventHandler(radGridView1_CellEditorInitialized);
}
 
void radGridView1_CellEditorInitialized(object sender, GridViewCellEventArgs e)
{
    RadDropDownListEditor editor = e.ActiveEditor as RadDropDownListEditor;
    if (editor == null)//this is not a DropDownEditor
    {
        return;
    }
 
    RadDropDownListEditorElement dropDownElement = (RadDropDownListEditorElement)editor.EditorElement;
    if (dropDownElement.Children[2].Children.Count != 2)//button already added! - exit
    {
        return;
    }
 
    RadButtonElement button = new RadButtonElement("X");
    button.StretchHorizontally = false;
    button.Click += new EventHandler(button_Click);
 
    dropDownElement.Children[2].Children.Add(button);           
}
I hope this helps.

Kind regards,
Peter
the Telerik team
Q3'12 SP1 of RadControls for WinForms is out now. See what's new.
0
John
Top achievements
Rank 1
answered on 04 Jan 2013, 07:32 PM
thanks peter, worked great.  you guys are always spot on.
0
John
Top achievements
Rank 1
answered on 14 Jan 2013, 08:11 PM
i ended up with one issue on this i can't get to work.  when the combo box column is set to the DropDownList style, this works great.
when it's set to DropDown though, i can't get my button to appear on top.  i've tried putting it in different children/doing a BringToFront() etc but i can't get the right combination.  see the image attached example for what i mean.

thanks.
0
Peter
Telerik team
answered on 16 Jan 2013, 01:24 PM
Hello John,

I was not able to reproduce the issue - please, refer to the attached video. 
Could you clarify which version of the controls do you use? 

As a workaround I would like to propose another solution based on CellBeginEdit and CellEndEdit events. 
Please, refer to the code below:
    this.radGridView1.CellEndEdit += new GridViewCellEventHandler(radGridView1_CellEndEdit);
    this.radGridView1.CellBeginEdit+=new GridViewCellCancelEventHandler(radGridView1_CellBeginEdit);
 
 
void radGridView1_CellBeginEdit(object sender, GridViewCellCancelEventArgs e)
{
    if (e.Column.Name == "column1")//you can exclude this check
    {
        RadDropDownListEditor editor = e.ActiveEditor as RadDropDownListEditor;
        if (editor != null)
        {
            RadButtonElement button = new RadButtonElement("X");
            button.Click += new EventHandler(button_Click);
            button.MaxSize = new System.Drawing.Size(20, 20);
            button.MinSize = new System.Drawing.Size(20, 20);
            button.Alignment = ContentAlignment.MiddleRight;
 
            RadDropDownListEditorElement dropDownElement = (RadDropDownListEditorElement)editor.EditorElement;                   
             
            dropDownElement.MaxSize = new Size(this.radGridView1.CurrentCell.Size.Width - 20, this.radGridView1.CurrentCell.Size.Height);
            this.radGridView1.CurrentCell.Children.Add(button);
        }
    }
}
 
void radGridView1_CellEndEdit(object sender, GridViewCellEventArgs e)
{
    if (e.Column.Name == "column1")//you can exclude this check
    {
        this.radGridView1.CurrentCell.Children.Clear();
    }
}
 
void button_Click(object sender, EventArgs e)
{
    MessageBox.Show("Click");
}

I hope this helps. All the best,
Peter
the Telerik team
Q3'12 SP1 of RadControls for WinForms is out now. See what's new.
0
John
Top achievements
Rank 1
answered on 16 Jan 2013, 09:28 PM
Thanks for the work around.  i was on 2012 q2 sp2.  i upgraded to the latest q3 release and it works like it should now without the workaround.
Tags
GridView
Asked by
John
Top achievements
Rank 1
Answers by
Peter
Telerik team
John
Top achievements
Rank 1
Share this question
or