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

type in combo box column

6 Answers 473 Views
GridView
This is a migrated thread and some comments may be shown as answers.
John
Top achievements
Rank 1
John asked on 07 Jan 2013, 09:22 PM
I have a column in a rad grid that's a combo box column.  i've loaded the datasource with stuff. it shows fine and users can pick one.  this column is filled with possible responses but they should also be able to type anything they want in the combo editor area and it retain that until they save it.  after that, the grid will be cleared and the drop downs loaded etc and return to the original state.  i can't get that to work though, after i type something in and leave, it disappears.  is there a way to allow users to pick something or type in it?

thanks

6 Answers, 1 is accepted

Sort by
0
Ivan Petrov
Telerik team
answered on 09 Jan 2013, 04:02 PM
Hello John,

Thank you for writing.

Please refer to this help article which explains how to allow users to add values in the drop down list editor of RadGridView.

I hope this will help. Should you have further questions, I would be glad to assist.
 
Greetings,
Ivan Petrov
the Telerik team
Q3'12 SP1 of RadControls for WinForms is out now. See what's new.
0
John
Top achievements
Rank 1
answered on 09 Jan 2013, 04:16 PM
Hi, thanks for the response.  i've looked at that article before.  I haven't actually tried to implement that but it appeared to me that the article actually adds it to the combo drop down.  i don't want to do that.  the drop down is a list of canned responses that never change.  if they want to use one, they pick it.  if they don't , they type what they want and upon saving to the db, that response goes away for ever.  it's never in the drop down.

i guess i could add it to the drop down and just never save the drop down data source.  there is a way on the form for them to add and save new items to that data source though and i'd have to be able to tell them apart.  that is probably do able, but it'd be much easier if i could just implement my original plan of typing in the combo box and it stays there until i save to the db.
0
Ivan Petrov
Telerik team
answered on 14 Jan 2013, 02:21 PM
Hello John,

Thank you for writing back.

To achieve your goal you can create a custom drop down list editor which returns as a value the text written in its text box.
public class CustomDropDownListEditor : RadDropDownListEditor
{
    public override object Value
    {
        get
        {
            RadDropDownListElement editor = this.EditorElement as RadDropDownListElement;
            return editor.TextBox.Text;
        }
        set
        {
            base.Value = value;
        }
    }
}

Then you can replace the default editor with yours, through the EditorRequired event of RadGridView:
private void radGridView1_EditorRequired(object sender, EditorRequiredEventArgs e)
{
    if (e.EditorType == typeof(RadDropDownListEditor))
    {
        e.Editor = new CustomDropDownListEditor();
    }
}

I hope this is useful. Should you have further questions, I would be glad to help.
 
Regards,
Ivan Petrov
the Telerik team
Q3'12 SP1 of RadControls for WinForms is out now. See what's new.
0
Brad
Top achievements
Rank 1
answered on 06 Feb 2013, 11:32 PM
Hello, 
I am using the CustomDropDownListEditor code snippet, in Ivan Petrov's post to save what a user enters into the GridComboBoxColumn.  The Value saves to the database successfully, however, the value is not being displayed by the Cell after the Edit is Ended, or when the form is reloaded. How can I get the value to show in the Cell when it is not in edit mode?  

The screen shot shows the record in the database and the form grid. 
0
Brad
Top achievements
Rank 1
answered on 07 Feb 2013, 03:21 AM

I have worked to implement a GridView Combo Box Column how I want it, similar to what the OP is asking for. The custom control works with a column's data source if it is a data table containing one string column. Cell values that are not in the data source are added when the column begins editing. Entered values that are not in the data source are added when the column ends editing. GridView events (CellFormating and CellEndEdit) sync the Cell Tag and Text values so that entered values display whether the grid is in ReadOnly mode or not. I implemented this because my form has an edit button that sets the gridview's read only property true and false.

Custom GridViewComboBox Editor Class:

public class GridViewComboBoxEditorExtended : RadDropDownListEditor
{
    DataTable dataTableRef = null;
    String column = null;
 
    public GridViewComboBoxEditorExtended(DataTable datasource, String valueColumn)
    {
        dataTableRef = datasource;
        column = valueColumn;
    }
    public override object Value
    {
        get
        {
            if (base.Value != null)
                return base.Value;
 
            RadDropDownListElement editor = this.EditorElement as RadDropDownListElement;
            return editor.TextBox.Text;
        }
        set
        {
            base.Value = value;
        }
    }
    public override void BeginEdit()
    {
 
        GridComboBoxCellElement cellElement = this.OwnerElement as GridComboBoxCellElement;
        if (cellElement.Tag == null || cellElement.Tag.ToString() == "")
        {
            base.BeginEdit();
            return;
        }
 
        // Checking if the typed value exists in the datasource of the column.
        for (int i = 0; i < dataTableRef.Rows.Count; i++)
        {
            if (dataTableRef.Rows[i][column].ToString() == cellElement.Tag.ToString())
            {
                base.BeginEdit();
                return;
            }
        }
 
        // An example of what we can do when we enter the custom text.
        // In this case we are adding a new data row in the underlying datasource of
        // the combobox column and then in the CellEndEdit we are setting
        // the ID value of the newly created row to RadGridView.
        DataRow newRow = dataTableRef.NewRow();
        //DataColumn newcol = new DataColumn(column);
        newRow[column] = cellElement.Tag;
        dataTableRef.Rows.Add(newRow);
 
        // Updating the database. You can do it here at another place
        // you find suitable for this purpose, for example, on FormClosing.
        //f.CategoriesTA.Update(f.DataSet.Categories);
 
        this.Value = cellElement.Tag;
 
        base.BeginEdit();
        return;
    }
    public override bool EndEdit()
    {
 
        GridComboBoxCellElement cellElement = this.OwnerElement as GridComboBoxCellElement;
        if (cellElement.Tag == null || cellElement.Tag.ToString() == "")
        {
            return base.EndEdit();
        }
 
        // Checking if the typed value exists in the datasource of the column.
        for (int i = 0; i < dataTableRef.Rows.Count; i++)
        {
            if (dataTableRef.Rows[i][column].ToString() == ((RadDropDownListEditorElement)this.EditorElement).Text)
            {
                return base.EndEdit();
            }
        }
 
        // An example of what we can do when we enter the custom text.
        // In this case we are adding a new data row in the underlying datasource of
        // the combobox column and then in the CellEndEdit we are setting
        // the ID value of the newly created row to RadGridView.
        DataRow newRow = dataTableRef.NewRow();
        //DataColumn newcol = new DataColumn(column);
        newRow[column] = ((RadDropDownListEditorElement)this.EditorElement).Text;
        dataTableRef.Rows.Add(newRow);
 
        // Updating the database. You can do it here at another place
        // you find suitable for this purpose, for example, on FormClosing.
        //f.CategoriesTA.Update(f.DataSet.Categories);
 
        cellElement.Tag = ((RadDropDownListEditorElement)this.EditorElement).Text;
 
        return base.EndEdit();
    }
}


GridView Implementation: 

private void radGridView1_EditorRequired(object sender, EditorRequiredEventArgs e)
{
    if (e.EditorType == typeof(RadDropDownListEditor)
        && radGridView1.CurrentColumn.Name == "ColumnName")
    {
        e.Editor = GridViewComboBoxEditorExtended(ColumnNameDataTable, "ColumnName");
    }
     
}
private void radGridView1_CellFormatting(object sender, CellFormattingEventArgs e)
{
    if (e.Column.Name == "ColumnName")
    {
        if (e.CellElement != null && e.Row is GridViewDataRowInfo)
        {
            e.CellElement.Text = e.CellElement.Value.ToString();
            e.CellElement.Tag = e.CellElement.Value.ToString();
        }
    }
}
private void radGridView1_CellEndEdit(object sender, GridViewCellEventArgs e)
{
    if (this.radGridView1.CurrentCell.Tag != null)
    {
        this.radGridView1.CurrentCell.Value = this.radGridView1.CurrentCell.Tag;
        this.radGridView1.CurrentCell.Tag = null;
    }
}
0
Ivan Petrov
Telerik team
answered on 11 Feb 2013, 12:04 PM
Hello Brad,

Thank you for writing.

I am glad you were able to find a solution to your case. I also want to thank you for sharing your solution with the community.

Do not hesitate to write back, should any questions arise.

Regards,
Ivan Petrov
the Telerik team
Q3'12 SP1 of RadControls for WinForms is out now. See what's new.
Tags
GridView
Asked by
John
Top achievements
Rank 1
Answers by
Ivan Petrov
Telerik team
John
Top achievements
Rank 1
Brad
Top achievements
Rank 1
Share this question
or