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
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
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
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; }
buyerId = value;
OnPropertyChanged(
"BuyerId"
);
public
int
Id
get
{
return
id; }
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
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
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
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
)
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
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
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