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

Horizontal Grid Line causing Custom Combo to Reset

3 Answers 42 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Erin Arroyo
Top achievements
Rank 1
Erin Arroyo asked on 07 Mar 2014, 01:39 PM
Hi,
I have a grid that I have bound my columns to a datareader.  I have 2 columns that I look at in this grid when wanting to make a custom combobox for a cell and they are "Batch Status" and "Status ID" and they are both textbox cell types.  For the column "Batch Status" I want to make it a combobox for certain cells if the "Batch Status" is "Open".

To start out I create a list array of all my combobox items.
Private mstrBatchStatusList As New List(Of ComboBoxDataSourceObject)()

Then I go ahead and use CellFormatting to make the cell a combobox if the Batch Status is "Open".
Private Sub rgvBatch_CellBeginEdit(ByVal sender As Object, ByVal e As Telerik.WinControls.UI.GridViewCellCancelEventArgs) Handles rgvBatch.CellBeginEdit
        '
        ' Got this from a website.  http://www.telerik.com/forums/dynamically-adding-controls-in-the-columns-in-the-grid
        ' The status column is a canceled Edit because I have substituted cell elements with my own elements
        ' (RadDropDownListEditorElement). In this way, I do not need to enter in edit mode and initialize the
        ' default column editor, because it is going to mess up with the new elements. I just use the elements'
        ' events to manage the change of the value and to save the cell value.
        '
        If e.Column.HeaderText = "Status" Then
            e.Cancel = True
        End If
    End Sub
    Private Sub rgvBatch_CellFormatting(ByVal sender As Object, ByVal e As Telerik.WinControls.UI.CellFormattingEventArgs) Handles rgvBatch.CellFormatting
        '
        ' Edit
        '
        If e.CellElement.ColumnInfo.HeaderText = "Edit" Then
            'This is how we get the RadButtonElement instance from the cell
            Dim button As RadButtonElement = CType(e.CellElement.Children(0), RadButtonElement)
            If e.CellElement.RowInfo.Cells("Batch_Status").Value IsNot Nothing Then
                Dim title As String = e.CellElement.RowInfo.Cells("Batch_Status").Value.ToString()
                If title = "Deleted" Then
                    button.Enabled = False
                Else
                    button.Enabled = True
                End If
            End If
        End If
        '
        ' Delete
        '
        If e.CellElement.ColumnInfo.HeaderText = "Delete" Then
            'This is how we get the RadButtonElement instance from the cell
            Dim button As RadButtonElement = CType(e.CellElement.Children(0), RadButtonElement)
            If e.CellElement.RowInfo.Cells("Batch_Status").Value IsNot Nothing Then
                Dim title As String = e.CellElement.RowInfo.Cells("Batch_Status").Value.ToString()
                If title <> "Open" Or mblnReadOnly Then
                    button.Enabled = False
                Else
                    button.Enabled = True
                End If
            End If
        End If
        '
        ' Status
        '
        If TypeOf e.CellElement.RowInfo Is GridViewDataRowInfo Then
            If e.CellElement.ColumnInfo.HeaderText = "Status" Then
                If e.CellElement.RowInfo.Cells("Batch_Status").Value IsNot Nothing Then
                    Dim strBatchStatus As String = e.CellElement.RowInfo.Cells("Batch_Status").Value.ToString()
                    Dim currentStatusID As Object = e.CellElement.RowInfo.Cells("AccPac_Batch_Status_ID").Value.ToString()
                    If strBatchStatus = "Open" And Not mblnReadOnly Then
                        Dim gvDropDownList As RadDropDownListEditorElement = Nothing
                        If e.CellElement.Children.Count = 0 OrElse Not (TypeOf e.CellElement.Children(0) Is RadDropDownListEditorElement) Then
                            e.CellElement.Children.Clear()
                            gvDropDownList = New RadDropDownListEditorElement()
                            For intI = 0 To mstrBatchStatusList.Count - 1
                                gvDropDownList.Items.Add(New RadListDataItem(mstrBatchStatusList.Item(intI).MyString, mstrBatchStatusList.Item(intI).Id))
                            Next
                            e.CellElement.Children.Add(gvDropDownList)
                            '
                            ' Preset the Drop down
                            '
                            If gvDropDownList IsNot Nothing Then
                                If currentStatusID IsNot Nothing Then
                                    gvDropDownList.SelectedValue = Convert.ToInt32(currentStatusID)
                                Else
                                    gvDropDownList.SelectedIndex = -1
                                End If
                            End If
                        Else
                            gvDropDownList = DirectCast(e.CellElement.Children(0), RadDropDownListEditorElement)
                        End If

                    ElseIf e.CellElement.Children.Count > 0 AndAlso TypeOf e.CellElement.Children(0) Is RadDropDownListEditorElement Then
                        '
                        ' Make sure any cells in the status column that are not "Open" status do not get changed to a combobox
                        '
                        e.CellElement.Children.Clear()
                    End If
                End If
            ElseIf e.CellElement.Children.Count > 0 AndAlso TypeOf e.CellElement.Children(0) Is RadDropDownListEditorElement Then
                '
                ' Make sure any other columns do not get changed to a combobox
                '
                e.CellElement.Children.Clear()
            End If
        End If
    End Sub

The above code works perfectly to setting the cell to a combobox when the Batch Status is "Open" and the drop down contains the correct values that are in my list array of (mstrBatchStatusList.Item.

Here are my questions:

1.  When I want to change my combobox value to a different item in the combobox, first I need to scroll to the right since my column you can't see.  I then change my value and scroll back to the left.  I then scroll back to the right and my combobox becomes reset to the original value.  It looks like upon scrolling right it calls the CellFormating function and when it gets to this line of the code (If e.CellElement.Children.Count = 0 OrElse Not (TypeOf e.CellElement.Children(0) Is RadDropDownListEditorElement) Then) it doesn't recognize that it's currently a combobox so it resets the combo box back to the original status id.

Why does this happen and how do I get this to not happen?

2.  If there an event that can be used that triggers a combo box change event so I can check the value it's changing to and set a flag that a value changed in the grid?

Thanks
 

3 Answers, 1 is accepted

Sort by
0
George
Telerik team
answered on 12 Mar 2014, 09:47 AM
Hello Erin,

Thank you for contacting us.

As far as I can see your goal is to have a DropDownEditor on the place of the TextEditor. If that is the case, then you need to use a custom editor, instead of adding and removing children in the CellFormating event. We have an article which can of help for this purpose - Using custom editors | UI for WinForms Documentation. Also the article Events | UI for WinForms Documentation can be found useful.

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

Regards,
George
Telerik
 

DevCraft Q1'14 is here! Watch the online conference to see how this release solves your top-5 .NET challenges. Watch on demand now.

 
0
Erin Arroyo
Top achievements
Rank 1
answered on 13 Mar 2014, 01:01 PM
Hi,

Thanks for your response and the custom editor worked much better than using the Cell Formatting but in the end I decided to go a different route.

I created the column from the beginning a GridViewComboBoxColumns.  Then at runtime I dynamically set the data source, value member, display member, and field name.  I then do a check and see if my status is "Open" I leave the combobox enabled else I disable the combo box so it only displays the text of my status.

Thanks again
0
George
Telerik team
answered on 18 Mar 2014, 08:25 AM
Hi Erin,

I am glad that you found a solution to the experienced issues.

Do not hesitate to contact us, should you have further questions.

Regards,
George
Telerik
 

DevCraft Q1'14 is here! Watch the online conference to see how this release solves your top-5 .NET challenges. Watch on demand now.

 
Tags
GridView
Asked by
Erin Arroyo
Top achievements
Rank 1
Answers by
George
Telerik team
Erin Arroyo
Top achievements
Rank 1
Share this question
or