Search code examples
c#gridviewdatagridviewcomboboxdevexpress-windows-ui

Add a combo box to a DevXpress grid dynamically on GridView_RowCellClick


I am using a "DevXpress.XtraGrid.GridView" and I have written following code to display a combo box on a cell. It happens when user clicks on the cell. In Debug or Running mode, code get executed properly but nothing happens. Have I done any mistake in the code? Please advice.

Here is my code:

    Private Sub GridView1_RowCellClick(sender As Object, e As RowCellClickEventArgs) Handles GridView1.RowCellClick
        Try
            Dim myNewLawyersCol As GridColumn = GridView1.Columns("NewLawyers")
            If (e.Column.Equals(myNewLawyersCol)) Then
                Dim riCombo As RepositoryItemComboBox = New RepositoryItemComboBox()
                riCombo.Items.Clear()
                riCombo.Items.AddRange(myList)

                GridControl1.RepositoryItems.Add(riCombo)
                GridView1.Columns("NewLawyers").ColumnEdit = riCombo
                GridControl1.Refresh()
            End If
        Catch ex As Exception
            DebugMessage(1, "Error in GridView1_RowCellClick :- " + ex.Message)
        Finally
            Cursor.Current = Cursors.Default
        End Try
    End Sub

Thank You.


Solution

  • Gosha (from DevExpress Support) helped me to sort the issue out.

    Here is his explanation:

    "When a user clicks a cell, an editor to edit that cell is created. As far as I understand, you need to change an editor for a particular cell to ComboBoxEdit. If so, the RowCellClick event is not a correct place to do this. To change the editor that will be used for editing a cell, use the GridView.CustomRowCellEditForEditing event. It's specially dedicated for this purpose."

    Here is my code:

        Private Sub GridView1_CustomRowCellEditForEditing(sender As Object, e As CustomRowCellEditEventArgs) Handles GridView1.CustomRowCellEditForEditing
            If (e.Column.FieldName = "NewLawyers") Then
                riCombo.Items.Clear()
                riCombo.Items.AddRange(allLawyersNames)
                e.RepositoryItem = riCombo
            End If
        End Sub