Validation on GridDateTimeColumn

7 posts, 0 answers
  1. Paul Taylor
    Paul Taylor avatar
    41 posts
    Member since:
    Jun 2010

    Posted 13 Dec 2011 Link to this post

    I am trying to programmatically add a RequiredFieldValidator to a GridDateTimeColumn. However, I get a null reference exception when the grid goes into Edit or Insert mode.

    Relevant code is:

                    <telerik:GridDateTimeColumn DataField="NoticeDate" HeaderText="Notice Date" UniqueName="NoticeDate"
                        DataFormatString="{0:d}" />

       Private Sub grdNip_ItemCreated(ByVal sender As Object, ByVal e As Telerik.Web.UI.GridItemEventArgs) Handles grdNip.ItemCreated
            If TypeOf e.Item Is GridEditableItem AndAlso e.Item.IsInEditMode Then
                Dim item As GridEditableItem = e.Item

                Dim dateEditor As GridDateTimeColumnEditor
                Dim valRequired As RequiredFieldValidator
                Dim cell As TableCell

                dateEditor = item.EditManager.GetColumnEditor("NoticeDate")
                cell = dateEditor.PickerControl.Parent
                dateEditor.PickerControl.ID = "dtNoticeDate"
                valRequired = New RequiredFieldValidator()
                valRequired.ControlToValidate = "dtNoticeDate"
                valRequired.ErrorMessage = "Notice date is required"
                cell.Controls.Add(valRequired)
            End If
        End Sub

    Stack trace:

    [NullReferenceException: Object reference not set to an instance of an object.]
       Telerik.Web.UI.GridDateTimeColumnEditor.set_Text(String value) +34
       Telerik.Web.UI.GridBoundColumn.OnDataBindColumn(Object sender, EventArgs e) +856
       System.Web.UI.Control.OnDataBinding(EventArgs e) +99
       System.Web.UI.Control.DataBind(Boolean raiseOnDataBinding) +92
       System.Web.UI.Control.DataBind() +15
       System.Web.UI.Control.DataBindChildren() +211
       System.Web.UI.Control.DataBind(Boolean raiseOnDataBinding) +102
       System.Web.UI.Control.DataBind() +15
       System.Web.UI.Control.DataBindChildren() +211
       System.Web.UI.Control.DataBind(Boolean raiseOnDataBinding) +102
       System.Web.UI.Control.DataBind() +15
       System.Web.UI.Control.DataBindChildren() +211
       System.Web.UI.Control.DataBind(Boolean raiseOnDataBinding) +102
       System.Web.UI.Control.DataBind() +15
       System.Web.UI.Control.DataBindChildren() +211
       System.Web.UI.Control.DataBind(Boolean raiseOnDataBinding) +102
       System.Web.UI.Control.DataBind() +15
       System.Web.UI.Control.DataBindChildren() +211
       System.Web.UI.Control.DataBind(Boolean raiseOnDataBinding) +102
       System.Web.UI.Control.DataBind() +15
       System.Web.UI.Control.DataBindChildren() +211
       System.Web.UI.Control.DataBind(Boolean raiseOnDataBinding) +102
       System.Web.UI.Control.DataBind() +15
       System.Web.UI.Control.DataBindChildren() +211
       System.Web.UI.Control.DataBind(Boolean raiseOnDataBinding) +102
       System.Web.UI.Control.DataBind() +15
       System.Web.UI.Control.DataBindChildren() +211
       System.Web.UI.Control.DataBind(Boolean raiseOnDataBinding) +102
       System.Web.UI.Control.DataBind() +15
       Telerik.Web.UI.GridEditFormItem.SetupItem(Boolean dataBind, Object dataItem, GridColumn[] columns, ControlCollection rows) +227
       Telerik.Web.UI.GridItemBuilder.CreateItems(GridGroupingContext group) +869
       Telerik.Web.UI.GridTableView.CreateItems(IEnumerator enumerator, GridColumn[] columns, ControlCollection controls) +129
       Telerik.Web.UI.GridTableView.CreateControlHierarchy(Boolean useDataSource) +994
       Telerik.Web.UI.GridTableView.CreateChildControls(IEnumerable dataSource, Boolean useDataSource) +545
       System.Web.UI.WebControls.CompositeDataBoundControl.PerformDataBinding(IEnumerable data) +57
       System.Web.UI.WebControls.DataBoundControl.OnDataSourceViewSelectCallback(IEnumerable data) +114
       System.Web.UI.DataSourceView.Select(DataSourceSelectArguments arguments, DataSourceViewSelectCallback callback) +31
       System.Web.UI.WebControls.DataBoundControl.PerformSelect() +142
       Telerik.Web.UI.GridTableView.PerformSelect() +16
       System.Web.UI.WebControls.BaseDataBoundControl.DataBind() +73
       Telerik.Web.UI.GridTableView.DataBind() +221
       Telerik.Web.UI.GridTableView.Rebind() +48
       Telerik.Web.UI.GridCommandEventArgs.ExecuteCommand(Object source) +159
       Telerik.Web.UI.RadGrid.OnBubbleEvent(Object source, EventArgs e) +134
       System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +37
       Telerik.Web.UI.GridItem.OnBubbleEvent(Object source, EventArgs e) +87
       System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +37
       System.Web.UI.WebControls.LinkButton.OnCommand(CommandEventArgs e) +118
       System.Web.UI.WebControls.LinkButton.RaisePostBackEvent(String eventArgument) +135
       System.Web.UI.WebControls.LinkButton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10
       System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
       System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +175
       System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1565
  2. Jayesh Goyani
    Jayesh Goyani avatar
    2732 posts
    Member since:
    May 2010

    Posted 13 Dec 2011 Link to this post

    Hello Paul,


    protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e)
        {
            if (e.Item is GridEditableItem && e.Item.IsInEditMode)
            {
                GridEditableItem item = e.Item as GridEditableItem;
      
                GridDateTimeColumnEditor dateEditor = item.EditManager.GetColumnEditor("NoticeDate") as GridDateTimeColumnEditor;
      
                if (dateEditor != null && dateEditor.PickerControl != null)
                {
                    TableCell cellstart = dateEditor.PickerControl.Parent as TableCell;
      
      
                    RequiredFieldValidator validatorstart = new RequiredFieldValidator();
                   validatorstart.ControlToValidate = dateEditor.PickerControl.ID;
                   validatorstart.ErrorMessage = "Your Message";
                    validatorstart.Display = ValidatorDisplay.Dynamic;
                    cellstart.Controls.Add(validatorstart);
                }
             
            }
        }


    Thanks,
    Jayesh Goyani
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Paul Taylor
    Paul Taylor avatar
    41 posts
    Member since:
    Jun 2010

    Posted 13 Dec 2011 Link to this post

    Thanks Jayesh, but apart from the fact that your example is in C# and you have added null tests on the editor and picker objects, I don't see any difference between your code and mine. I have tried adding the same null tests, but I still get the same exception (during the ItemCreated event, both those objects have a value). Any idea why I get an exception? Have you tested your code and do you get an exception?
  5. Jayesh Goyani
    Jayesh Goyani avatar
    2732 posts
    Member since:
    May 2010

    Posted 13 Dec 2011 Link to this post

    Hello Paul,

    Please check below code snippet.
    below code is working perfectly from my side.
    <telerik:RadGrid ID="RadGrid1" runat="server" AutoGenerateColumns="false" OnNeedDataSource="RadGrid1_NeedDataSource"
               onitemcreated="RadGrid1_ItemCreated">
               <MasterTableView >
                   <Columns>
                         <telerik:GridDateTimeColumn DataField="NoticeDate" HeaderText="Notice Date" UniqueName="NoticeDate"
                       DataFormatString="{0:d}" />
                       <telerik:GridEditCommandColumn></telerik:GridEditCommandColumn>
                   </Columns>
               </MasterTableView>
                      </telerik:RadGrid>
    protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
        {
            dynamic data = new[] {
                    new { ID1 = 1, CustomerName = 2,NoticeDate=DateTime.Now},
                     new { ID1 = 1, CustomerName = 2,NoticeDate=DateTime.Now},
                     new { ID1 = 1, CustomerName = 2,NoticeDate=DateTime.Now},
                     new { ID1 = 1, CustomerName = 2,NoticeDate=DateTime.Now},
                     new { ID1 = 1, CustomerName = 2,NoticeDate=DateTime.Now},
                     new { ID1 = 1, CustomerName = 2,NoticeDate=DateTime.Now},
                    new { ID1 = 4, CustomerName = 5,NoticeDate=DateTime.Now}
                    
                };
            RadGrid1.DataSource = data;
        }
     
     
    protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e)
        {
            if (e.Item is GridEditableItem && e.Item.IsInEditMode)
            {
                GridEditableItem item = e.Item as GridEditableItem;
     
                GridDateTimeColumnEditor dateEditor = item.EditManager.GetColumnEditor("NoticeDate") as GridDateTimeColumnEditor;
     
                if (dateEditor != null && dateEditor.PickerControl != null)
                {
                    TableCell cellstart = dateEditor.PickerControl.Parent as TableCell;
     
     
                    RequiredFieldValidator validatorstart = new RequiredFieldValidator();
                    validatorstart.ControlToValidate = dateEditor.PickerControl.ID;
                    validatorstart.ErrorMessage = "Your Message";
                    validatorstart.Display = ValidatorDisplay.Dynamic;
                    cellstart.Controls.Add(validatorstart);
                }
     
            }
        }


    Note : if you still get the issue then please let me know from which line number you get this error.

    Thanks,
    Jayesh Goyani
  6. Paul Taylor
    Paul Taylor avatar
    41 posts
    Member since:
    Jun 2010

    Posted 13 Dec 2011 Link to this post

    Hi Jayesh,

    After translating your code to VB, I managed to find the source of the error. In my original code, I set the PickerControl ID, following examples I had seen for GridTextBoxColumnEditors.

    dateEditor.PickerControl.ID = "dtNoticeDate"

    If you change your code to include this line, you will get the same error. I believe this is a bug.

    Cheers,
    Paul Taylor
  7. Jayesh Goyani
    Jayesh Goyani avatar
    2732 posts
    Member since:
    May 2010

    Posted 13 Dec 2011 Link to this post

    Hello Paul,

    Are you able to resolved your issue ???

    Thanks,
    Jayesh Goyani
  8. Paul Taylor
    Paul Taylor avatar
    41 posts
    Member since:
    Jun 2010

    Posted 13 Dec 2011 Link to this post

    Jayesh, yes, thanks. When I remove the line that set the ID of the Picker control, the exception no longer occurs.
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017