Need to set default value to RadComboBoxColumn

6 posts, 0 answers
  1. Sebastian
    Sebastian avatar
    4 posts
    Member since:
    Dec 2013

    Posted 30 Sep 2019 Link to this post

    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

  2. Jesse
    Jesse avatar
    10 posts
    Member since:
    Aug 2018

    Posted 01 Oct 2019 in reply to Sebastian Link to this post

    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);
  3. Sebastian
    Sebastian avatar
    4 posts
    Member since:
    Dec 2013

    Posted 01 Oct 2019 in reply to Jesse Link to this post

    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!

  4. Jesse
    Jesse avatar
    10 posts
    Member since:
    Aug 2018

    Posted 01 Oct 2019 Link to this post

    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;
                }
            }
        }
    }
  5. Nadya
    Admin
    Nadya avatar
    379 posts

    Posted 02 Oct 2019 Link to this post

    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.
  6. Sebastian
    Sebastian avatar
    4 posts
    Member since:
    Dec 2013

    Posted 02 Oct 2019 in reply to Nadya Link to this post

    Thank you Jesse and Nadya for your quick help!!
Back to Top