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

Reply: RadComboBoxColumn add/write items

10 Answers 195 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Marcin
Top achievements
Rank 1
Marcin asked on 11 Nov 2010, 10:42 PM
Hello,
i have RadComboboxColumn in my grid. I set DropDownStyle : DropDown and AutoCompletedMode : SuggestAppend. I bind this column to list with some numbers. Now, I wolud like to create event: when i write another number in Combobox (list don't containts this number), number will be add to combox. I have problem becouse when i write to cell some text and go to next cell event EndEdit doesn't work. What is more when i use event f.e CellFormatting and write some text Cell.Value is always null.
Any idea to solve this problem ?
    Thanks

10 Answers, 1 is accepted

Sort by
0
Accepted
Emanuel Varga
Top achievements
Rank 1
answered on 11 Nov 2010, 11:40 PM
Hello Marcin,

Please take a look at the following example, sorry it is not very clean but it will work:
using System;
using System.ComponentModel;
using System.Linq;
using System.Windows.Forms;
using Telerik.WinControls.UI;
 
public partial class Form1 : Form
{
    private RadGridView radGridView1;
    private BuyersCollection buyers = new BuyersCollection(10, 10);
 
    public Form1()
    {
        InitializeComponent();
        this.Controls.Add(radGridView1 = new RadGridView());
        radGridView1.Dock = DockStyle.Fill;
        radGridView1.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill;
 
        this.radGridView1.DataBindingComplete += new GridViewBindingCompleteEventHandler(radGridView1_DataBindingComplete);
        radGridView1.CellEditorInitialized += new GridViewCellEventHandler(radGridView1_CellEditorInitialized);
    }
 
    void radGridView1_CellEditorInitialized(object sender, GridViewCellEventArgs e)
    {
        if (e.Column.Name == "SomeComboboxColumn")
        {
            var editor = e.ActiveEditor as RadDropDownListEditor;
            var editorElement = editor.EditorElement as RadDropDownListEditorElement;
            editorElement.DataSource = buyers;
            editorElement.DropDownStyle = Telerik.WinControls.RadDropDownStyle.DropDown;
            editorElement.SelectedValueChanged += new Telerik.WinControls.UI.Data.ValueChangedEventHandler(editorElement_SelectedValueChanged);
        }
    }
 
    void editorElement_SelectedValueChanged(object sender, Telerik.WinControls.UI.Data.ValueChangedEventArgs e)
    {
        var editorElement = sender as RadDropDownListEditorElement;
        if (editorElement.SelectedValue != null || string.IsNullOrEmpty(editorElement.EditableElementText))
        {
            return;
        }
 
        //check if buyers already contains an item with that text
        if (buyers.Any(o => o.Name.Equals(editorElement.EditableElementText)))
        {
            return;
        }
 
        // create the new object here
        var buyer = new Buyer(buyers.Count + 10, editorElement.EditableElementText);
        buyers.Add(buyer);
        radGridView1.CurrentCell.Value = buyer.Id;
    }
 
    protected override void OnLoad(EventArgs e)
    {
        base.OnLoad(e);
 
        this.radGridView1.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill;
        this.radGridView1.DataSource = new ProductsCollection(10);
    }
 
    void radGridView1_DataBindingComplete(object sender, GridViewBindingCompleteEventArgs e)
    {
        radGridView1.Columns["BuyerId"].IsVisible = false;
 
        var column = new GridViewComboBoxColumn("SomeComboboxColumn", "SomeComboboxColumn");
        column.DataSource = buyers;
 
        column.ValueMember = "Id";
        column.FieldName = "BuyerId";
 
        column.DisplayMember = "Name";
        this.radGridView1.Columns.Add(column);
 
        this.radGridView1.BestFitColumns();
    }
}
 
#region Helpers
 
public class Product : INotifyPropertyChanged
{
    private int id, buyerId;
 
    public int BuyerId
    {
        get { return buyerId; }
        set
        {
            buyerId = value;
            OnPropertyChanged("BuyerId");
        }
    }
 
    public int Id
    {
        get { return id; }
        set
        {
            id = value;
            OnPropertyChanged("Id");
        }
    }
 
    public Product(int id, int buyerId)
    {
        this.Id = id;
        this.BuyerId = buyerId;
    }
 
    private void OnPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
 
    public event PropertyChangedEventHandler PropertyChanged;
}
 
public class Buyer
{
    public int Id
    {
        get;
        set;
    }
 
    public string Name
    {
        get;
        set;
    }
 
    public Buyer(int id, string name)
    {
        this.Id = id;
        this.Name = name;
    }
}
 
public class ProductsCollection : BindingList<Product>
{
    public ProductsCollection(int noItems)
    {
        for (int i = 0; i < noItems; i++)
        {
            this.Add(new Product(i, i + 10));
        }
    }
}
 
public class BuyersCollection : BindingList<Buyer>
{
    public BuyersCollection(int startIndex, int lenght)
    {
        for (int i = 0; i < 10; i++)
        {
            this.Add(new Buyer(i + 10, "Buyer" + (i + 1)));
        }
    }
}
 
#endregion Helpers

Hope this helps, if you have any other questions or comments, please let me know,

Best Regards,
Emanuel Varga

Telerik WinForms MVP
0
Marcin
Top achievements
Rank 1
answered on 12 Nov 2010, 09:07 AM
Thanks for this example.
0
Accepted
Emanuel Varga
Top achievements
Rank 1
answered on 12 Nov 2010, 09:47 AM
Hello,

The snipped I've provided is working under Q3 2010, please change that line to:
var editor = radGridView1.ActiveEditor as RadDropDownListEditor;

Hope this helps, if you have any other questions or comments, please let me know,

Best Regards,
Emanuel Varga
Telerik WinForms MVP
0
Marcin
Top achievements
Rank 1
answered on 12 Nov 2010, 10:05 AM
Ok, it's work. Thanks !
0
Emanuel Varga
Top achievements
Rank 1
answered on 12 Nov 2010, 10:19 AM

Glad to be able to help,

If you have any more questions please just let me know, and if the question has been solved, please mark the all of the helpfull posts as answers, so that others can find the answers to their questions faster.

Best Regards,
Emanuel Varga
Telerik WinForms MVP
0
Marcin
Top achievements
Rank 1
answered on 15 Nov 2010, 09:52 AM
Hello,
can I create for new object in combobox hyperlink? f.e when i write a new value in combobox this text will be hyperlink and when i Click link open f.e MessageBox with information ?
Thanks for help.
0
Emanuel Varga
Top achievements
Rank 1
answered on 15 Nov 2010, 02:53 PM
Hello again,

Sorry, there is no hyperlink for the combo, but i would suggest just using a button to the right of the combobox, or just the SelectedValueChanged event or SelectedIndexChanged event to fire an action when the item has been changed.

Hope this helps, if you have any other questions or comments, please let me know,

Best Regards,
Emanuel Varga
Telerik WinForms MVP
0
Andy
Top achievements
Rank 1
answered on 17 Nov 2010, 08:17 PM
I actually think this example is quite clean and very useful...

I'm attempting to do a similar, task, but instead of adding an item to the RadDropDownListElement, I'm attempting to change the selected value of the dropdownlist if some business logic condition is true.  In stepping through the code, the line:

 

editor.SelectedValue = radGridOrderLines.CurrentCell.Value;

Seems to have no effect. The statement generates no error, but the value of editor.SelectedValue still contains its orginial value. here would be an example of the full procedure for my case:

void editorElement_SelectedValueChanged(object sender, Telerik.WinControls.UI.Data.ValueChangedEventArgs e) 
  
    {
        var editorElement = sender as RadDropDownListEditorElement;
        if (editorElement.SelectedValue != null || string.IsNullOrEmpty(editorElement.EditableElementText))
        {
            return;
        }
   
        //Do some business logic checking
        if (<some-condition> is true)
        {
                 MessageBox.Show ("Sorry, this is not a valid selection.");
                 //The Line below seems to have no effect!!!!
                 editor.SelectedValue = radGridOrderLines.CurrentCell.Value;
        
    }



A second question with this same example would be where would be the best recommended place to remove the SelectedValueChanged Delegate?
 

Thanks!

0
Dipak
Top achievements
Rank 1
answered on 08 Dec 2010, 12:16 AM
Hi
Thanks for this nice example. I have one issue if the cell does not have any value in it then editorElement_SelectedValueChanged event is not firing.Could you please give me any workaround for that?
Thanks
Dipak
0
Alexander
Telerik team
answered on 14 Dec 2010, 10:48 AM
Hello,

Jeff, the code from your snippet does not change the SelectedValue of the editor because this operation is suspended while the previously started one is still in progress. It means that you cannot use the SelectedValueChanged event handler to set different SelectedValue to the RadDropDownListEditor. I would suggest that implement your logic using the ValueChanging event of RadGridView. You can cancel this event to keep the previously selected value.

Dipak, when a GridViewComboBoxColumn cell value is null and you change it to another value, the SelectedValueChanged event of the editor element will fire. Please give us more details for your case so we can assist you further.

Best regards,
Alexander
the Telerik team
Get started with RadControls for WinForms with numerous videos and detailed documentation.
Tags
GridView
Asked by
Marcin
Top achievements
Rank 1
Answers by
Emanuel Varga
Top achievements
Rank 1
Marcin
Top achievements
Rank 1
Andy
Top achievements
Rank 1
Dipak
Top achievements
Rank 1
Alexander
Telerik team
Share this question
or