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

ComboBox Binding

2 Answers 207 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Deborah
Top achievements
Rank 1
Deborah asked on 21 Oct 2010, 01:08 AM
We have a very odd requirement. The grid rows show existing data, which is not editable. It is provided for display purposes only. The user can then add rows. One of the columns is a combobox that is bound to a list of items.

The odd requirement is that the comboBox needs to provide a subset of the data when it is dropped down for editing.

Here is a scenario. The grid contains the following customer names and types:
  • Jones   Education Customer
  • Smith   Business Customer
  • Baggins Residentical Customer
  • Support System Customer

In order to get the customer type name to show up (instead of the customer type id) we need to bind the combobox to the full set of customer types. But when the user adds a new one, they cannot have the System Customer as an option in the list.

I am filtering out the items that we don't want and rebinding it. But the new list is ignored and the full list is populated.

Any tips?

My code is below:

Private Sub CustomerGridView_CellBeginEdit(ByVal sender As Object, ByVal e As Telerik.WinControls.UI.GridViewCellCancelEventArgs) Handles CustomerGridView.CellBeginEdit
    ' The list of types available for selection is different from the list that is
    ' bound for display, so handle this here.
    Dim DropDownListEditor As RadDropDownListEditor = TryCast(Me.CustomerGridView.ActiveEditor, RadDropDownListEditor)
    If DropDownListEditor IsNot Nothing Then
        If (CustomerGridView.Columns(e.ColumnIndex).Name = "Type") Then
            Dim editorElement As RadDropDownListEditorElement = CType(DropDownListEditor.EditorElement, RadDropDownListEditorElement)
            Dim filteredList = OriginalList.
                Where(Function(d) d.IsSystem = false)
            editorElement.DataSource = filteredList.ToList
        End If
    End If
End Sub

2 Answers, 1 is accepted

Sort by
0
Emanuel Varga
Top achievements
Rank 1
answered on 21 Oct 2010, 08:57 AM
Hello Deborah,

Again there is an issue with the asynchronous nature of the layout system used in RadGridView and the TPF (in my opinion)

Please see the following example:
void radGridView1_CellBeginEdit(object sender, GridViewCellCancelEventArgs e)
{
    if (radGridView1.Columns[e.ColumnIndex].Name != "Type")
    {
        return;
    }
 
    var dropDownEditor = radGridView1.EditorManager.ActiveEditor as RadDropDownListEditor;
    if (dropDownEditor != null)
    {
        var editor = dropDownEditor.EditorElement as RadDropDownListEditorElement;
        var newDataSource = typesList.Where(str => !str.Equals("admin")).ToArray();
 
        // this should work but it doesn't
        //Application.DoEvents();
        //radGridView1.RootElement.UpdateLayout();
        //editor.DataSource = newDataSource;
 
        // stupid alternative but it will work
        var timer = new Timer();
        timer.Interval = 1;
        timer.Tick += delegate
        {
            editor.DataSource = newDataSource;
            timer.Stop();
        };
 
        timer.Start();
    }
}

I might be doing something wrong but i couldn't find any other way...

Just for the record, i know and agree, stupid workaround

Best Regards,
Emanuel Varga
0
Alexander
Telerik team
answered on 26 Oct 2010, 10:09 AM
Hello Deborah,

Thank you for your question.

I would advise you to use the CellEditorInitialized event of RadGridView to define the DataSource of the editor:

Private Sub RadGridView1_CellEditorInitialized(ByVal sender As Object, ByVal e As Telerik.WinControls.UI.GridViewCellEventArgs) Handles RadGridView1.CellEditorInitialized
    Dim editor As RadDropDownListEditor = TryCast(Me.RadGridView1.ActiveEditor, RadDropDownListEditor)
    If editor IsNot Nothing Then
        Dim editorElement As RadDropDownListEditorElement = TryCast(editor.EditorElement, RadDropDownListEditorElement)
        editorElement.DataSource = filteredList
    End If
End Sub

I hope it helps.

Best regards,
Alexander
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
Tags
GridView
Asked by
Deborah
Top achievements
Rank 1
Answers by
Emanuel Varga
Top achievements
Rank 1
Alexander
Telerik team
Share this question
or