Initializing a column editor is a mystery

7 posts, 0 answers
  1. Dave Hayward
    Dave Hayward avatar
    40 posts
    Member since:
    Jul 2012

    Posted 31 Dec 2009 Link to this post

    I couldn't find any examples, or forum posts, so I thought I'd ask:

    I'm trying to initialize a DateTimeColumnEditor to todays date after the user presses the 'Insert' button. I assume the correct place to do this is the 'ItemCommand' handler making sure it's handling a 'InitInsertCommandName'.

    When I try to find the column editor using Radgrid1.MasterTableView.GetInsertItem(), I get an exception, claiming that the grid isn't in insert mode. Since this handler only gets called once for this command, I assume there's a different magic spell I need to perform this task.

    Can anyone help?

    TIA 
  2. Dave Hayward
    Dave Hayward avatar
    40 posts
    Member since:
    Jul 2012

    Posted 03 Jan 2010 Link to this post

    Okay, so I found the examples for initializing grid items. I am still a bit confused by the 'DefaultinsertValue' parameter for the GriddateColumn (and a few others) type. This takes a parameter of type 'Object' by doesn't support the DataBind event. This seems to limit it's usefullness a bit. Are there any recommendations for its use?
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Tsvetoslav
    Admin
    Tsvetoslav avatar
    1823 posts

    Posted 05 Jan 2010 Link to this post

    Hi Dave,

    Could you explain in detail what you mean by the DefaultInserValue property not supporting the DataBind event and what exactly the issue is about the DefaultInserValue propety expecting an object for its value?

    Thanks.

    Best  wishes,
    Tsvetoslav
    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.
  5. Dave Hayward
    Dave Hayward avatar
    40 posts
    Member since:
    Jul 2012

    Posted 05 Jan 2010 Link to this post

    Tsvetoslav,

    Thanks for your response. I was trying to find a simple way to initialize a GridDateTimeColumn to today's date when a new record was being created. As an early attempt I thought the DefaultInsertValue seemed promising, so I tried something like:

     

    <telerik:GridDateTimeColumn DataFormatString="{0:D}" DataField="ExerciseDate" HeaderText="Date"

     

     

    PickerType="DatePicker" UniqueName="ExerciseDate" EditFormHeaderTextFormat="{0:D}"

     

     

    ItemStyle-Width="100px" DefaultInsertValue='<%# System.DateTime.Today %>' >

     

     

    </telerik:GridDateTimeColumn>

     


    When the page loads, you get an error message (see errormsg.png) stating that the GridDateTimeColumn does not have a Databind event. This was a surprising message, but it only occurred when trying to databind the DefaultInsertValue.

    I then went seeking more information about the DefaultInsertvalue, assuming I was just using it improperly. I found some reference to it on the following page: http://www.telerik.com/help/aspnet-ajax/grdinsertingvaluesinplaceandeditforms.html . Toward the bottom of that page, I found the reference to the DefaultInsertvalue with the statement that it is of type System.Object (see helptext.png). I was confused by that too, but since I'm new to using the Rad controls, I'm confused by more things than I understand.

    In any case, I was just wondering how DefaultInsertvalue was supposed to be used.

    Thanks for you help.

    Dave
  6. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 06 Jan 2010 Link to this post

    Hello Dave,

    You can preset the DateTimeColumn editor value form code behind as shown below:
    c#:
    protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e) 
        { 
            if (e.Item is GridEditFormInsertItem && e.Item.OwnerTableView.IsItemInserted) 
            { 
                GridEditFormInsertItem insertItem = (GridEditFormInsertItem)e.Item; 
                RadDatePicker datepicker = (RadDatePicker)insertItem["DateTimeColumnUniqueName"].Controls[0];             
                datepicker.SelectedDate = (DateTime)DateTime.Today.Date; 
            } 
        } 

    -Princy.
  7. Tsvetoslav
    Admin
    Tsvetoslav avatar
    1823 posts

    Posted 08 Jan 2010 Link to this post

    Hi Dave,

    The grid datetime column does not support binding expressions, therefore you cannot set its properties in the mark-up using binding expressions. However, you can set the default insert value for the column in code-behind, as follows:

    protected void RadGrid1_ItemCommand(object source, GridCommandEventArgs e)
     {
         if (e.CommandName == RadGrid.InitInsertCommandName)
             ((GridDateTimeColumn)e.Item.OwnerTableView.GetColumnSafe("ColumnUniqueName")).DefaultInsertValue = DateTime.Now;
     }

    Alternatively, you can take the approach described in the last paragraph of the help article you mentioned:

    protected void RadGrid1_ItemCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
    {
      if (e.CommandName == RadGrid.InitInsertCommandName)
      {
        // cancel the default operation
        e.Canceled = true;
      
        //Prepare an IDictionary with the predefined values
        System.Collections.Specialized.ListDictionary newValues = new System.Collections.Specialized.ListDictionary();
         newValues["ColumnUniqueName"] = DateTime.Now;
      
         //Insert the item and rebind
         e.Item.OwnerTableView.InsertItem(newValues);
      
       }
    }

    A third valid and viable approach is the one described by Princy.

    I hope this information helps.

    Greetings,
    Tsvetoslav
    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.
  8. Dave Hayward
    Dave Hayward avatar
    40 posts
    Member since:
    Jul 2012

    Posted 08 Jan 2010 Link to this post

    Tsvetoslav and Princy,

    Thanks to you both for your help. My code is working much better and the fog of battle is lifting a bit as well.

    Dave
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017