I have a scenario where I need to populate a combobox in a cell based on the selection of another cell's combo box. That is, for example, there is a ShippingCarrier (UPS, USPS, FedEx etc) and when user select one of them, it should populate next cell's combo box with their services (for example, if UPS is selected, UPS Priority, UPS Next Day Air etc should be available in second column).
I am using UnBound grid. My code seems to work, HOWEVER, when data is populated in second column, you can not drop down 2nd combo box using mouse (the only way to drop down is to put cursor on second drop down and use ALT+Down Arrow key). Below is the code I am using.
col = AppendNewColumn(ColumnType.DropDown, 100, "Carrier", "Carrier") |
Dim cCol As GridViewComboBoxColumn = CType(col, GridViewComboBoxColumn) |
cCol.DataSource = GetShippingCarriers() |
cCol.DisplayMember = "Display" |
cCol.ValueMember = "Key" |
AppendNewColumn is the function which creates column and add that to columns collection of Grid. next you can see that it get shipping carriers and bind to that.
Now in CellEndEdit event, I am trying to populate second column as below.
Private Sub grdMain_CellEndEdit(ByVal sender As Object, ByVal e As Telerik.WinControls.UI.GridViewCellEventArgs) Handles grdMain.CellEndEdit |
If e.ColumnIndex = 12 Then 'Shipping Carrier Selected |
Dim row As GridViewRowInfo = Me.grdMain.Rows(e.RowIndex) |
Dim newCarrier As String = row.Cells(e.ColumnIndex).Value.ToString |
If newCarrier IsNot Nothing AndAlso newCarrier <> "" Then 'Next column is Shipping Method (e.ColumnIndex + 1) |
Dim cellElem As GridComboBoxCellElement = CType(row.Cells(e.ColumnIndex + 1).CellElement, GridComboBoxCellElement) |
Debug.Write(cellElem.ToString) |
Dim col As GridViewDataColumn = CType(cellElem.ColumnInfo, GridViewDataColumn) |
Dim element As New RadComboBoxElement |
ServiceInfo.UPSServices.ToArray() |
element.DataSource = GetShippingServices(CType(CInt(newCarrier), ShippingCarriers)) |
element.DisplayMember = "DisplayName" |
element.StretchHorizontally = True |
element.StretchVertically = True |
element.DropDownStyle = Telerik.WinControls.RadDropDownStyle.DropDownList |
For Each obj As Object In cellElem.Children |
If TypeOf obj Is ComboBoxEditorLayoutPanel Then |
cellElem.Children.Remove(CType(obj, ComboBoxEditorLayoutPanel)) |
End If |
Next |
'cellElem.Children.Clear() |
cellElem.Children.Add(element) |
Debug.Write(col.FieldName) |
End If |
End If |
Is there anything wrong with the above code that drop down is NOT selected correctly using mouse?
Also, as you can see that I am binding using name/value pair, however, from the cell object, I can get only Value, is there any way to get SelectedItem (Drop down item selected) which may further give me Text and Value? For example, row.Cells(e.ColumnIndex).Value.ToString returns value (numeric), however, display thing is Text (USPS, UPS etc). Is there any way to get either DisplayName or ValueMember value?
Thanks,
Sameers