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

Default Cell Value on edit

1 Answer 215 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Albert
Top achievements
Rank 1
Albert asked on 16 Nov 2009, 08:49 PM

During the CellBeginEdit event I need to supply a default value for the cell if there is no data in the cell and only during an Edit event. I initialized the default value during the CellEditorInitialized event and all works, the default value appears.
 

 

 

Private Sub gv_CellEditorInitialized(ByVal sender As ObjectByVal e As Telerik.WinControls.UI.GridViewCellEventArgs) Handles gv.CellEditorInitialized  
    If TypeOf gv.ActiveEditor Is RadTextBoxEditor Then 
        gv.ActiveEditor.Value = IIf(object.Data= 0, "",object.Data)  
    End If 
End Sub

Now when the Enter Key is pressed (accepting the default value) the CellEndEdit event is fired and I have the following code:
If IsNumeric(e.Value.ToString) Then 
  object.data = e.Value  
End If 
The issue is that the Cell Value (e.value) is never equal to the default value, it's empty.

Any help greatly appreciated.



1 Answer, 1 is accepted

Sort by
0
Jack
Telerik team
answered on 18 Nov 2009, 01:37 PM
Hi Code7,

Thank you for contacting us. I understand what you want to achieve. However, when setting the editor value this way it assumes that this is the value currently saved in the cell. This prevents saving the value when handling CellEndEdit. You have two options:

1. Change the cell value when handling CellBeginEdit event:

Private Sub radGridView1_CellBeginEdit(ByVal sender As Object, ByVal e As GridViewCellCancelEventArgs)
    If TypeOf Me.radGridView1.ActiveEditor Is RadTextBoxEditor AndAlso Me.radGridView1.CurrentCell.Value = DBNull.Value Then
        Me.radGridView1.CurrentCell.Value = "a"
    End If
End Sub

2. Use a custom editor that sets the value after processing BeginEdit method:

Private Sub radGridView1_EditorRequired(ByVal sender As Object, ByVal e As EditorRequiredEventArgs)
    If e.EditorType Is GetType(RadTextBoxEditor) Then
        e.EditorType = GetType(CustomTextBoxEditor)
    End If
End Sub
 
Public Class CustomTextBoxEditor
    Inherits RadTextBoxEditor
    Private nullValue As Boolean
   
    Public Overloads Overrides Property Value() As Object
        Get
            Return MyBase.Value
        End Get
        Set(ByVal value As Object)
            nullValue = value = DBNull.Value
            MyBase.Value = value
        End Set
    End Property
   
    Public Overloads Overrides Sub BeginEdit()
        MyBase.BeginEdit()
        If nullValue Then
            Me.Value = "a"
        End If
    End Sub
End Class

I hope this helps. If you need further assistance, please write back.

Best wishes,
Jack
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
Tags
GridView
Asked by
Albert
Top achievements
Rank 1
Answers by
Jack
Telerik team
Share this question
or