Programatically setting the value of a GridViewComboBoxColumn

2 posts, 1 answers
  1. melkorman
    melkorman avatar
    16 posts
    Member since:
    Aug 2009

    Posted 20 Jan 2012 Link to this post

    Hi,

    I have a GridView that is unbound. I manually add the rows and the columns, one of which is a GridViewComboBoxColumn (other columns omitted):

    var departmentColumn = new GridViewComboBoxColumn();
     
    departmentColumn.HeaderText = "Department";
    departmentColumn.Name = "departmentColumn";
    departmentColumn.Width = 120;
    departmentColumn.DisplayMember = "Name";
    departmentColumn.ValueMember = "Id";
    departmentColumn.DataSource = _departments;
     
    grid.MasterTemplate.Columns.AddRange(new GridViewDataColumn[]
                                                     {
                                                         departmentColumn
                                                     });

    _departments is a list containing a number of Department objects, which have among other things a Name property and an Id property.

    I manually add the rows to the grid like this (still, other columns omitted):

    var row = grid.Rows.AddNew();
    row.Cells[0].Value = "Department A";

    I would like this to result in a new row in the grid, where "Department A" is selected in the list of values in the GridViewComboBoxColumn. However, this doesn't work. Trying to set the name results in a FormatException ("Input string was not in a correct format.")

    I feel silly for having to ask because it must be dead simple, but I am stuck!

    Best regards
  2. Answer
    Stefan
    Admin
    Stefan avatar
    2908 posts

    Posted 25 Jan 2012 Link to this post

    Hi Linus,

    Thank you for writing.

    The reason for the observed behavior is that you have set the ValueMember of the column to "Id", which means that when you want to add new record, you should specify it by id, and not by its name:

    var row = radGridView1.Rows.AddNew();
    row.Cells[0].Value = "1";

    If you want to add records by their name, simply change the ValueMember property to "Name".

    I hope that the provided information addresses your question. If you need further assistance, do not hesitate to contact us.

    Regards,

    Stefan
    the Telerik team

    SP1 of Q3’11 of RadControls for WinForms is available for download (see what's new).

Back to Top