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
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