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

Populating a combo dynamically in RadGridView

5 Answers 631 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Mark
Top achievements
Rank 1
Mark asked on 02 Apr 2018, 06:11 PM

I have a blank RadGridView.  The user will click a button and I want to add a new row to the grid.  The first column is a textbox, second column is a GridViewMultiComboBox.  When they click the button, I can't figure out where I populate just the combobox with data. 

Once they select the entry in the combo box, I then need to get data from the database for the text box column.

any help is greatly appreciated.

Thanks!

Mark

 

5 Answers, 1 is accepted

Sort by
0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 04 Apr 2018, 08:57 AM
Hello, Mark,   

GridViewMultiComboBox has a RadMultiColumnComboBoxElement as an editor. In order to populate the column with data it is necessary to set the GridViewMultiComboBox.DataSource property. The DisplayMember property specifies what data to be displayed in the cells and the ValueMember property indicates what field from the source will be used to store the values in the cells from this column. Then, it is necessary to set a valid value to the cell according to the applied ValueMember of the column. Thus, the correct DisplayMember will be shown as text. Additional information how to setup the column is available in the following help article: https://docs.telerik.com/devtools/winforms/gridview/columns/column-types/gridviewmulticomboboxcolumn

In order to detect the moment when a new value is selected from the RadMultiColumnComboBoxElement editor while it is still active, you can handle the ValueChanged event. You can get the editor's value by accessing the RadGridView.ActiveEditor.Value property and then update the desired cell of the current row.

I hope this information helps. If you have any additional questions, please let me know. 

Regards,
Dess
Progress Telerik
Try our brand new, jQuery-free Angular components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
0
Mark
Top achievements
Rank 1
answered on 04 Apr 2018, 11:24 AM

I understand all that but how do I manually create the row with the combo.  I know how to populate it but I can't figure how to do it manually.

something like this is what I am looking for. 

gvAdmin.Rows.Add("", dt)

dt is a datatable to bind the combo with.

0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 05 Apr 2018, 01:33 PM
Hello, Mark,   
  
You can create a new row in the grid and specify a value for the cell belonging to the GridViewMultiComboBox. You can't pass a DataSource collection when you add a new row. If I understand your requirement correctly, you want to have a specific DataSource collection for each cell in the GridViewMultiComboBox. For this purpose I would recommend you to use a GridViewTextBoxColumn and handle the EditorRequired event where you can replace the default editor with a RadMultiColumnComboBoxElement. Then, in the CellEditorInitialized event you can set the RadMultiColumnComboBoxElement's DataSource, DisplayMember and ValueMember properties of the editor in order to load the desired data in the editor.

However, if it is not the exact requirement, please specify in details what is the exact requirement that you are trying to achieve. Thus, we would think about a suitable solution and assist you further.

I hope this information helps. If you have any additional questions, please let me know. 

Regards,
Dess
Progress Telerik
Try our brand new, jQuery-free Angular components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
0
Mark
Top achievements
Rank 1
answered on 05 Apr 2018, 01:47 PM

I thought my original post would explain what I want.  So I will try again.  In the server code behind, I need to DYNAMICALLY add a row to the gridview.  The first column is a TextBoxColumn and will be blank.  The second column is a GridViewMultiComboBox.  When I dynamically add the row, I need the combobox populated with a datatable and I have the code to do that.

My problem is I don't know how to do this.  Telerik documentation has nothing that shows how to populate a GridViewMultiComboBox dynamically in the code behind.  I need to only create a row when the user clicks the add button.  See screen shot.  I am trying to write the code to do that and have not figured out how.

Thanks in advance.

0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 06 Apr 2018, 11:46 AM
Hello, Mark,   
 
According to the provided information I am not sure how exactly you fill the columns in RadGridView. I suppose that you add them programmatically. The following help articles demonstrate how to add a GridViewTextBoxColumn and a GridViewMultiComboBoxColumn  respectively:
https://docs.telerik.com/devtools/winforms/gridview/columns/column-types/gridviewtextboxcolumn
https://docs.telerik.com/devtools/winforms/gridview/columns/column-types/gridviewmulticomboboxcolumn

In the help article about GridViewMultiComboBoxColumn it is demonstrated how to set the DataSource for the column and specify what to be displayed in the cells and stored in the cells by setting the DisplayMember and ValueMember properties of the column. Note that the RadMultiColumnComboBoxElement will be visible only when the cell is in edit mode. Otherwise, you will see plain text. Here is demonstrated a sample code snippet how to setup the columns and add several rows to the grid. When the user select a new item in the RadMultiColumnComboBoxElement, the text cell is automatically populated. I have attached my sample project for your convenience.
private void RadForm1_Load(object sender, EventArgs e)
{
    GridViewTextBoxColumn textColumn = new GridViewTextBoxColumn("Description");
    textColumn.Width = 200;
    this.radGridView1.Columns.Add(textColumn);
 
    GridViewMultiComboBoxColumn col = new GridViewMultiComboBoxColumn();
    col.DataSource = GetDataSource();
    col.DisplayMember = "Name";
    col.ValueMember = "Id";
    col.Width = 200;
    this.radGridView1.Columns.Add(col);
 
    this.radGridView1.ValueChanged+=radGridView1_ValueChanged;
 
    this.radGridView1.Rows.Add("one",2);
    this.radGridView1.Rows.Add("two",4);
}
 
private object GetDataSource()
{
    DataTable dt = new DataTable();
    dt.Columns.Add("Id",typeof(int));
    dt.Columns.Add("Name",typeof(string));
    for (int i = 0; i < 5; i++)
    {
        dt.Rows.Add(i,"Item"+i);
    }
    return dt;
}
 
private void radGridView1_ValueChanged(object sender, EventArgs e)
{
    //set the value for the text column
    this.radGridView1.CurrentRow.Cells["Description"].Value = DateTime.Now.ToLongTimeString();
}

If you are still experiencing any further difficulties, feel free to modify my sample project in a way to replicate the undesired behavior and get back us with it so we can investigate the precise case. 

I hope this information helps. If you have any additional questions, please let me know. 

Regards,
Dess
Progress Telerik
Try our brand new, jQuery-free Angular components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
Tags
GridView
Asked by
Mark
Top achievements
Rank 1
Answers by
Dess | Tech Support Engineer, Principal
Telerik team
Mark
Top achievements
Rank 1
Share this question
or