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

Programatically setting the value of a GridViewComboBoxColumn

1 Answer 223 Views
GridView
This is a migrated thread and some comments may be shown as answers.
melkorman
Top achievements
Rank 1
melkorman asked on 20 Jan 2012, 02:26 PM
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

1 Answer, 1 is accepted

Sort by
0
Accepted
Stefan
Telerik team
answered on 25 Jan 2012, 09:59 AM
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).

Tags
GridView
Asked by
melkorman
Top achievements
Rank 1
Answers by
Stefan
Telerik team
Share this question
or