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

how to dynamically add DataGridViewComboBoxCell in telerik gridviews?

15 Answers 552 Views
GridView
This is a migrated thread and some comments may be shown as answers.
hosein
Top achievements
Rank 1
hosein asked on 20 Oct 2010, 06:11 PM
Hi All

I need to dynamically add  DataGridViewComboBoxCell in telerik datagridview but I could not do that
is there any way to add such cells dynamically?

thanks

 

15 Answers, 1 is accepted

Sort by
0
Emanuel Varga
Top achievements
Rank 1
answered on 20 Oct 2010, 10:17 PM
Hello hosein,

You cannot add a windows forms DataGridView cell to the RadGridView, but if you want to create a GridViewComboBoxColumn for the RadGridView dynamically, you can take a look at the following help article.

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

Best Regards,
Emanuel Varga
0
hosein
Top achievements
Rank 1
answered on 25 Oct 2010, 09:49 PM
thank you for answer
but I could not find any solution for My Problem

I want ot dynamically add data into combobox in telerik gridview cells
Can you help me a little more about it?

thanks in advance
0
Emanuel Varga
Top achievements
Rank 1
answered on 25 Oct 2010, 10:44 PM
Hello again,

Do you want just to add a GridViewComboBoxColumn to the grid with a set of value, or do you need a specific set of values for each row based on a condition? If you need the latter, please take a look at this thread.

If you just want a normal column with a data source please take a loot at this example:
using System;
using System.ComponentModel;
using System.Windows.Forms;
using Telerik.WinControls.UI;
 
public partial class Form1 : Form
{
    private RadGridView radGridView1;
 
    public Form1()
    {
        InitializeComponent();
        this.Controls.Add(radGridView1 = new RadGridView());
        this.radGridView1.DataBindingComplete += new GridViewBindingCompleteEventHandler(radGridView1_DataBindingComplete);
        radGridView1.Dock = DockStyle.Fill;
    }
 
    protected override void OnLoad(EventArgs e)
    {
        base.OnLoad(e);
 
        this.radGridView1.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill;
        this.radGridView1.DataSource = new ProductsCollection(1000);
    }
 
    void radGridView1_DataBindingComplete(object sender, GridViewBindingCompleteEventArgs e)
    {
        radGridView1.Columns["BuyerId"].IsVisible = false;
 
        var column = new GridViewComboBoxColumn("SomeComboboxColumn", "SomeComboboxColumn");
        column.DataSource = new BuyersCollection(10, 10);
 
        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
0
hosein
Top achievements
Rank 1
answered on 26 Oct 2010, 02:35 PM

I want to have comboboxColumn but The the cells can have different values (In your code we only can have equal values)
I want to bind different ArrayList Objects (for example) to the ComboBox Cell for each different row
I couldn't find out how to do that from the thread you mentioned.
In .NET datagridview I used this code to do that

public void displayDGV(DataGridView DGV, ArrayList list, string strColumnName, string strHeader)
{                        
      DGV.Columns.Add(strColumnName, strHeader);                
      DataGridViewComboBoxCell cmbcell = new DataGridViewComboBoxCell();
      if (list != null && list.Count != 0)
      {
            for (int j = 0; j < list.Count; j++)
            {
                cmbcell.Items.Add((string)list[j]);
            }
                cmbcell.Value = list[0];
      
      DGV[strColumnName, m] = cmbcell;            
}
Thanks
0
Nikolay
Telerik team
answered on 28 Oct 2010, 04:01 PM
Hi hosein,

Thank you for the question.

In order to achieve the desired behavior, you should bind the combo column to one data source and bind the editor of the column to certain parts of the main data source. This is demonstrated in the attached sample project.

I hope this helps.

Best wishes,
Nikolay
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
0
hosein
Top achievements
Rank 1
answered on 30 Oct 2010, 08:06 PM
thank you for your answer
for each row (each record), I have different values that may not have any common data but in your code all rows have the same values
for instance, I have a column named "Phones". each record has different number of values for that column like below:

ID        col1        col2        col3        Phones 
1         first1       first2       first3         001,002,003  (comboboxCell)
2         sec2        sec2       sec3         004,005,006  (comboboxCell)
3         third1     third2      third3         007,010  (comboboxCell)
4         four1       four2       four3         008,000  (comboboxCell)

how can I bind an arraylist of data to each telerik comboboxCell? (DataGridViewRadComboBoxCell)
0
Emanuel Varga
Top achievements
Rank 1
answered on 30 Oct 2010, 08:25 PM
Hello again,

In my reply with the example , i have also provided a link where you can find a solution if you want differend data sources / row.
You can handle that like i said here, or using the CellEditorInitialized event, like Alexander said, something like this:
void radGridView1_CellEditorInitialized(object sender, GridViewCellEventArgs e)
{
    if (e.Column.Name == "SomeComboboxColumn")
    {
        var dropDownEditor = radGridView1.ActiveEditor as RadDropDownListEditor;
        var dropDownEditorElement = dropDownEditor.EditorElement as RadDropDownListEditorElement;
        var buyers = new BuyersCollection(e.RowIndex, e.RowIndex + 2);
        dropDownEditorElement.DataSource = buyers;
        dropDownEditorElement.DisplayMember = "Name";
        dropDownEditorElement.ValueMember = "Id";
    }
}

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

Best Regards,
Emanuel Varga
0
hosein
Top achievements
Rank 1
answered on 10 Nov 2010, 05:29 PM
Hi
Thank you for answer
but can you use these codes in a simple c# project.
I could not use it correctly
I will be appreciated you if you do that.
thanks again
0
Richard Slade
Top achievements
Rank 2
answered on 10 Nov 2010, 05:40 PM
Hello hosein, 

There was a C# project attached in this thread by Nikolay. See above. 
Hope that helps
Richard
0
Accepted
Emanuel Varga
Top achievements
Rank 1
answered on 10 Nov 2010, 05:46 PM
Hello again,

Of course i can, please take a look at the following example:
using System;
using System.ComponentModel;
using System.Windows.Forms;
using Telerik.WinControls.UI;
 
public partial class Form1 : Form
{
    private RadGridView radGridView1;
 
    public Form1()
    {
        InitializeComponent();
        this.Controls.Add(radGridView1 = new RadGridView());
        radGridView1.Dock = DockStyle.Fill;
        radGridView1.CellEditorInitialized += new GridViewCellEventHandler(radGridView1_CellEditorInitialized);
        this.radGridView1.DataBindingComplete += new GridViewBindingCompleteEventHandler(radGridView1_DataBindingComplete);
    }
 
    protected override void OnLoad(EventArgs e)
    {
        base.OnLoad(e);
        this.radGridView1.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill;
        this.radGridView1.DataSource = new ProductsCollection(10);
    }
 
    void radGridView1_CellEditorInitialized(object sender, GridViewCellEventArgs e)
    {
        if (e.Column.Name == "SomeComboboxColumn")
        {
            var dropDownEditor = radGridView1.ActiveEditor as RadDropDownListEditor;
            var dropDownEditorElement = dropDownEditor.EditorElement as RadDropDownListEditorElement;
            var buyers = new BuyersCollection(Convert.ToInt32(e.Value));
            dropDownEditorElement.DataSource = buyers;
            dropDownEditorElement.DisplayMember = "Name";
            dropDownEditorElement.ValueMember = "Id";
        }
    }
 
    void radGridView1_DataBindingComplete(object sender, GridViewBindingCompleteEventArgs e)
    {
        radGridView1.Columns["BuyerId"].IsVisible = false;
 
        var column = new GridViewComboBoxColumn("SomeComboboxColumn", "SomeComboboxColumn");
        column.DataSource = new BuyersCollection(10);
        column.ValueMember = "Id";
        column.FieldName = "BuyerId";
        column.DisplayMember = "Name";
        this.radGridView1.Columns.Add(column);
 
        this.radGridView1.BestFitColumns();
    }
}
 
#region Helpers
 
public class Product
{
    public int Id
    {
        get;
        set;
    }
 
    public int BuyerId
    {
        get;
        set;
    }
 
    public Product()
    {
    }
 
    public Product(int id, int buyerId)
    {
        this.Id = id;
        this.BuyerId = buyerId;
    }
}
 
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)
    {
        for (int i = startIndex; i < startIndex + 10; i++)
        {
            this.Add(new Buyer(i, "Buyer " + (i + 1).ToString()));
        }
    }
}
 
#endregion Helpers

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

Best Regards,
Emanuel Varga
0
hosein
Top achievements
Rank 1
answered on 19 Nov 2010, 06:30 AM
hi
it works

thank you very much
0
Kif
Top achievements
Rank 1
answered on 21 Feb 2011, 03:57 PM

Hi,

I have a similar problem as the original poster. The code in the last post from Emanuel Varga almost solve the issue, but it does not solve populating the grid with existing data. Try to change the input to the BuyersCollection() constructor in radGridView1_DataBindingComplet():
 
From:
column.DataSource = new BuyersCollection(10);
To:
column.DataSource = new BuyersCollection(5);

The result is that the last five rows show empty values in the "SomeComboboxColumn". I assume this is because the list currently bound to this column does not contain the missing values.

So the remaining question is: How do I populate a grid when one column contain values from many different (dynamic) lists?
 

Regards,
K

0
Martin Vasilev
Telerik team
answered on 24 Feb 2011, 01:44 PM
Hi Kif,

You get the described empty rows, because changing the code to "column.DataSource = new BuyersCollection(5)" leaves some column values without a pair in its data source.

In order to allow for showing the right text in the combo-box column, all of its values should be presented in its data source.

All the best,
Martin Vasilev
the Telerik team
Registration for Q1 2011 What’s New Webinar Week is now open. Mark your calendar for the week starting March 21st and book your seat for a walk through all the exciting stuff we ship with the new release!
0
Kif
Top achievements
Rank 1
answered on 24 Feb 2011, 02:03 PM
Hi,

I know the cell becomes empty because it's value is not present in the data source. That's what I wanted to demonstrate by the code change.

My problem was when the grid was initially populated with data. In that case the CellEditorInitialized was not yet called, and hence the datasource was empty.

I found a different approach to this issue in another thread, which use the CellBeginEdit and CellFormatting events. So far it seems to work fine.

Regards,
K

0
Martin Vasilev
Telerik team
answered on 01 Mar 2011, 05:49 PM
Hi Kif,

I am glad you have found a solution in the given forum thread. Do not hesitate to contact us again if you have any other questions.

All the best,
Martin Vasilev
the Telerik team
Registration for Q1 2011 What’s New Webinar Week is now open. Mark your calendar for the week starting March 21st and book your seat for a walk through all the exciting stuff we ship with the new release!
Tags
GridView
Asked by
hosein
Top achievements
Rank 1
Answers by
Emanuel Varga
Top achievements
Rank 1
hosein
Top achievements
Rank 1
Nikolay
Telerik team
Richard Slade
Top achievements
Rank 2
Kif
Top achievements
Rank 1
Martin Vasilev
Telerik team
Share this question
or