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

Need to set default value to RadComboBoxColumn

5 Answers 567 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Sebastian
Top achievements
Rank 1
Sebastian asked on 30 Sep 2019, 12:21 PM

Hi support team,

I'm trying to set the default value for a RadComboBoxColumn (inside a RadGridView) when a new row is created but I cannot figure out how to do it.

I'm using Telerik 2018.2.515.40

5 Answers, 1 is accepted

Sort by
0
Jesse
Top achievements
Rank 1
answered on 01 Oct 2019, 05:38 PM

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);
0
Sebastian
Top achievements
Rank 1
answered on 01 Oct 2019, 05:48 PM

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!

0
Jesse
Top achievements
Rank 1
answered on 01 Oct 2019, 06:08 PM

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;
            }
        }
    }
}
0
Nadya | Tech Support Engineer
Telerik team
answered on 02 Oct 2019, 06:56 AM

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

Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
0
Sebastian
Top achievements
Rank 1
answered on 02 Oct 2019, 01:23 PM
Thank you Jesse and Nadya for your quick help!!
Tags
GridView
Asked by
Sebastian
Top achievements
Rank 1
Answers by
Jesse
Top achievements
Rank 1
Sebastian
Top achievements
Rank 1
Nadya | Tech Support Engineer
Telerik team
Share this question
or