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

How to assign selected text from a GridViewComboBoxColumn to its containing cell

1 Answer 107 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Shamjith
Top achievements
Rank 1
Shamjith asked on 02 Nov 2009, 06:22 PM
Hi,
     There are two columns in a grid placed in a windows form. The first column is a GridViewComboBoxColumn and the second one is a GridViewDataColumn. I have populated the combo box with values. I need to assign the selected value from the drop down to the cell on which it resides. The code i have written is given below.

private void BindData()
        {
            radGridView1.GridElement.BeginUpdate();
            var gridViewLookUpColumn = ((GridViewLookUpColumn)(radGridView1.CurrentRow.Cells["column1"].ColumnInfo));

            if (gridViewLookUpColumn == null) return;

            gridViewLookUpColumn.DataSource = FillData();
            gridViewLookUpColumn.DisplayMember = "Name";
            gridViewLookUpColumn.ValueMember = "Id";
            gridViewLookUpColumn.DropDownStyle = RadDropDownStyle.DropDownList;

            radGridView1.GridElement.EndUpdate();
        }

        private static DataTable FillData()
        {
            var dt = new DataTable();
            dt.Columns.Add(new DataColumn("Name", typeof(String)));
            dt.Columns.Add(new DataColumn("Id", typeof(Int32)));

            var dr = dt.NewRow();
            dr["Name"] = "Test 1";
            dr["Id"] = 1;
            dt.Rows.Add(dr);

            dr = dt.NewRow();
            dr["Name"] = "Test 2";
            dr["Id"] = "2";
            dt.Rows.Add(dr);
            return dt;
        }

 private void radGridView1_CellBeginEdit(object sender, GridViewCellCancelEventArgs e)
        {
            BindData();
        }

        private void radGridView1_CellEndEdit(object sender, GridViewCellEventArgs e)
        {
            try
            {
                var radEditor = radGridView1.ActiveEditor;

                if (radEditor == null) return;
                switch (radEditor.GetType().Name)
                {
                    case "RadComboBoxEditor":
                        {

                            var element = ((RadComboBoxElement)((RadComboBoxEditor)radGridView1.ActiveEditor).EditorElement);
                           
                            if (element == null) return;
                            var selectedText = ((RadComboBoxItem) element.SelectedItem).Text;
                            MessageBox.Show("Begin" + selectedText);

                            radGridView1.CurrentRow.Cells["column1"].CellElement.Text = selectedText;
                            //radGridView1.Rows[e.RowIndex + 1].Cells[e.ColumnIndex + 1].Value = element.SelectedValue;                                
                            break;
                        }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }


I can select the value from the dropdown. But the value assigned to the cell is not displaying. Please help me.

Regards
Shamjith


1 Answer, 1 is accepted

Sort by
0
Jack
Telerik team
answered on 04 Nov 2009, 10:36 AM
Hello Shamjith,

The value should be updated automatically. You have to set the DataType for GridViewComboBoxColumn to integer. Consider the following code snippet:

this.radGridView1.Columns.Add(new GridViewTextBoxColumn("column0"));
this.radGridView1.Columns.Add(new GridViewLookUpColumn("column1"));
this.radGridView1.Columns["column1"].DataType = typeof(int);
 
this.radGridView1.MasterGridViewTemplate.AutoGenerateColumns = false;
this.radGridView1.DataSource = FillData();

I hope this helps. Should you have any questions, feel free to ask.

Greetings,
Jack
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
Tags
GridView
Asked by
Shamjith
Top achievements
Rank 1
Answers by
Jack
Telerik team
Share this question
or