5 Answers, 1 is accepted
Sebastian,
Depending on whether you have a databound object or not, you can accomplish this by using the FieldName property to bind the combobox to a value from the data bound object.
Example:
var newColumn =
new
GridViewComboBoxColumn();
((GridViewComboBoxColumn)newColumn).DataSource = SomeListofModelObjects;
newColumn.FieldName =
"Phase"
;
// This will bind the combobox to the data source field bound to the grid
newColumn.DisplayMember =
"Phase"
;
// This is the source display name from the DataSource for the combobox.
newColumn.ValueMember =
"PhaseKey"
;
// This is the value from the datasource for the combobox, but is also passed to the datagrid data source object field you specified earlier.
sparePartsItemDataGrid.Columns.Add(newColumn);
Hi Jesse, thank you for your reply.
Sorry if I was not clear. I have a dropdown column (not bounded) with three possible values and what I need to do is to set a default item selected when a new row is created.
Thanks in advance!
Sebastian,
Depending on what is bound to your combo box in the grid, you should be able to bind to the CreateRow event, then set the value. In my case my combobox value is an integer but you might be a string. Obviously alter the code for the appropriate checks and such.
private
void
radGridView1_CreateRow(
object
sender, Telerik.WinControls.UI.GridViewCreateRowEventArgs e)
{
if
(e.RowInfo !=
null
)
{
foreach
(GridViewCellInfo cell
in
e.RowInfo.Cells)
{
if
(cell.ColumnInfo.GetType() ==
typeof
(GridViewComboBoxColumn))
{
cell.Value = 23;
}
}
}
}
Hello,
Jesse, thank you for the provided solutions. CreateRow event will fire for all the rows in the grid. In this case, I can suggest you to use the DefaultValuesNeeded event in order to set the default values in RadGridView. DefaultValuesNeeded event fires when the user enters the row for new records so that it can be populated with default values. Please refer to the following code snippet which result is demonstrated in the attached gif file:
private void RadGridView1_DefaultValuesNeeded(object sender, GridViewRowEventArgs e)
{
e.Row.Cells["ComboBoxColumn"].Value = "Mr.";
}
I hope this helps. Should you have any other questions, I will be glad to help.
Regards,
Nadya
Progress Telerik