Hi.
I have a RadGridView which has a GridViewComboBoxColumn. ComboBoxColumn.DataSource is an array of Products. Product is an object that has a few properties like Id, Name, and so on. How i can get ComboBoxColumn selected item?
I have tried figure it out by this way
radGridView1.Rows[index].Cells["ComboBoxColumn Name"].Value but it returns only string, not Product object
Can you help me figure it out?
8 Answers, 1 is accepted
Thank you for writing.
The editor is only accessible while the cell is beeing edited. And only the selected value is stored. This is why you will need to use this value and manually retrieve the business object from the data source of the column.
I hope this helps.
Regards,
Dimitar
Telerik by Progress
Hi Dimitar.
Thank you for your answer.
I have a one more question about column sorting.
I have an another RadGridView that have a BindingSource typeof(Product). when i click in column header columns sorting works only on primitive types. Let me say if my Product object has a Category object which is in grid appears in TextBoxColumn and have DataType Category, i can't sort it by clicking in Category Column.Your business object should implement the IComparable interface. Here is a simple example:
class
MyClass : IComparable
{
public
string
Text {
get
;
set
; }
public
int
CompareTo(
object
obj)
{
return
(
this
.Text.CompareTo(obj.ToString()));
}
public
override
string
ToString()
{
return
this
.Text;
}
}
Do not hesitate to contact us if you have other questions.
Dimitar
Telerik by Progress
Hi Dimitar, thank you for solution. it's working.
Now i have another problem with GridViewTextBoxColumn. Im my RadGridView i have a GridViewTextBoxColumn and its MultiLine property set to true. What exactly i want to do... When i editing the cell of GridViewTextBoxColumn and press Enter button I want to add some text into cell and keep it in Edit mode.
I have tried to handle RadGridView.KeyPressed and KeyDown event. but they are firing only when cell is already edited.
In code language i want to do similar with this.
private void radGridView1_CellValueChanged(object sender, GridViewCellEventArgs e)
{
if (e.Column.Name == "SerialNumbers")
{
radGridView1.CurrentCell.Value = radGridView.CurrentCell.Value.Tostring + Enviroment.NewLine;
radGridView.CurrentCell.BeginEdit();
}
}
Make sure that the AcceptsReturn property of the column is set:
var col = radGridView1.Columns[1]
as
GridViewTextBoxColumn;
col.AcceptsReturn =
true
;
col.Multiline =
true
;
You can use the CellEditorInitialized event to set the editor value:
private
void
RadGridView1_CellEditorInitialized(
object
sender, GridViewCellEventArgs e)
{
if
(e.Column.Name ==
"Name"
)
{
e.ActiveEditor.Value =
"test"
;
}
}
I hope this helps.
Regards,
Dimitar
Telerik by Progress
I am not sure why the appropriate text is not displayed. In general, it should be displayed automatically. Perhaps it would be better to open e new thread for this and send us the code for the column initialization and a screenshot of the actual result.
In addition, please make sure that the DisplayMember/ValueMember properties are set correctly.
I hope this will be useful.
Regards,
Dimitar
Telerik by Progress