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

Initializing a column editor is a mystery

6 Answers 188 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Dave Hayward
Top achievements
Rank 1
Dave Hayward asked on 31 Dec 2009, 04:32 PM
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 

6 Answers, 1 is accepted

Sort by
0
Dave Hayward
Top achievements
Rank 1
answered on 03 Jan 2010, 03:43 PM
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?
0
Tsvetoslav
Telerik team
answered on 05 Jan 2010, 01:59 PM
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.
0
Dave Hayward
Top achievements
Rank 1
answered on 05 Jan 2010, 09:22 PM
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
0
Princy
Top achievements
Rank 2
answered on 06 Jan 2010, 07:57 AM
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.
0
Tsvetoslav
Telerik team
answered on 08 Jan 2010, 08:30 AM
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.
0
Dave Hayward
Top achievements
Rank 1
answered on 08 Jan 2010, 07:25 PM
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
Tags
Grid
Asked by
Dave Hayward
Top achievements
Rank 1
Answers by
Dave Hayward
Top achievements
Rank 1
Tsvetoslav
Telerik team
Princy
Top achievements
Rank 2
Share this question
or