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

Get ComboBox Value

3 Answers 326 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, 11:46 PM
I know this question has been answered many times before, but none of those replies are working for me.

I have a GridView with a TextBox column. Under specific circumstances, the TextBox column becomes a DropDown list. I have this part working fine.

I bind the DropDownList to a List<T> where T is an object, say CustomerType for example. Normally, I would bind the DisplayMember to the "TypeName" property and the ValueMember to the "CustomerTypeId" property and all would be well.

But because the TextBox column is bound to the "TypeName" of the object instead of the Id, I need some code to get the Id from the combobox.

I tried adding code to the ValueChanged event, but I am always getting the wrong element. Here is my code:

Private Sub CustomerGridView_ValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles CustomerGridView.ValueChanged
    If CustomerGridView.CurrentRow IsNot Nothing AndAlso
            Me.CustomerGridView.Columns(COL_customerType).IsCurrent Then
        Dim editor = TryCast(sender, RadDropDownListEditor)
        If editor IsNot Nothing Then
            Dim editorElement = TryCast(editor.EditorElement, RadDropDownListEditorElement)
            If editorElement IsNot Nothing Then
                Dim selectedItem = TryCast(editorElement.SelectedItem.DataBoundItem, CustomerType)
                If selectedItem IsNot Nothing The
                    Dim currentCustomer = TryCast(CustomerGridView.CurrentRow.DataBoundItem, Customer)
                    Customer.CustomerTypeId = selectedItem.CustomerTypeId
                End If
            End If
        End If
    End If
End Sub


Any tips?

3 Answers, 1 is accepted

Sort by
0
Emanuel Varga
Top achievements
Rank 1
answered on 22 Oct 2010, 01:32 AM
Hello Deborah, have you tried using the CellValueChanged event to get the last value? Sorry, I cannot test it right now, but as soon as I get the chance I will and I will get back to you. Best Regards, Emanuel Varga
0
Deborah
Top achievements
Rank 1
answered on 22 Oct 2010, 05:59 AM
Yes. I did try that. At the time that the CellValueChanged event is generated, the editor seems to be already closed. So there I can't get the selected value.

But I was able to get this to work (after spending all darn day on it!)

Here is what I did:
  • Changed the column back to a ComboBox in the designer.
  • When it is an existing value, set it as readonly in the CellFormatting event.
  • Do NOT set the FieldName for the column, but set the DisplayMember and ValueMember for the ComboBox.
  • In the Cell Formatting event (which is one of the few that has access to the Text property of the control), set the Text property for the cell when it is marked as read-only.

I did not have time to fully test it before I had to leave today ... but it seemed to work OK.

The CellFormatting event handles the field when it is read-only (for an existing value.)
And the ComboBox sets the value when it is not read-only (for a new value.)

0
Jack
Telerik team
answered on 27 Oct 2010, 08:46 AM
Hi Deborah,

I am glad to hear that you have found a solution. The other possible option is to use the EditorRequired event to replace the default editor for the column. In this case I am using a text box column. Please consider the following code snippet:
Private Sub RadGridView1_EditorRequired(ByVal sender As Object, ByVal e As Telerik.WinControls.UI.EditorRequiredEventArgs) Handles RadGridView1.EditorRequired
    If TypeOf Me.RadGridView1.CurrentRow Is GridViewDataRowInfo AndAlso CInt(Me.RadGridView1.CurrentRow.Cells(0).Value) Mod 2 = 0 Then
        e.EditorType = GetType(RadDropDownListEditor)
    End If
End Sub
 
Private Sub RadGridView1_CellBeginEdit(ByVal sender As Object, ByVal e As Telerik.WinControls.UI.GridViewCellCancelEventArgs) Handles RadGridView1.CellBeginEdit
    Dim editor As RadDropDownListEditor = TryCast(Me.RadGridView1.ActiveEditor, RadDropDownListEditor)
    If editor IsNot Nothing Then
        Dim editorElement As RadDropDownListEditorElement = DirectCast(editor.EditorElement, RadDropDownListEditorElement)
 
        Dim data As New DataTable()
        data.Columns.Add("ID", GetType(Integer))
        data.Columns.Add("Name", GetType(String))
        data.Rows.Add(1, "one")
        data.Rows.Add(2, "two")
        data.Rows.Add(3, "three")
 
        editorElement.DisplayMember = "Name"
        editorElement.ValueMember = "Name"
        editorElement.ListElement.BindingContext = Me.RadGridView1.BindingContext
        editorElement.DataSource = data
    End If
 
End Sub

I hope it helps. This code snippet has been tested with our latest release Q2 2010 SP2.

Should you have any further questions, do not hesitate to ask.

Regards,
Jack
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
Deborah
Top achievements
Rank 1
Jack
Telerik team
Share this question
or