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

Modifying GridDateTimeColumnEditor timeview

5 Answers 109 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Jonathan
Top achievements
Rank 1
Jonathan asked on 26 Sep 2012, 01:50 PM
I currently have a GridDateTimeColumn with PickerType = DateTimePicker. I want to change the timeview to display in 2 columns instead of the default 3. I've searched the forums and found what I thought should be solutions but the code seems to have no effect. 
(http://www.telerik.com/community/forums/aspnet-ajax/grid/many-grid-questions.aspx ; http://www.telerik.com/community/forums/aspnet-ajax/grid/how-to-localize-datetimepicker-s-tooltip-in-filter-menu.aspx )


In the RadGrid1_ItemDataBound event I have the following code:

GridEditableItem item = (GridEditableItem)e.Item;
 
RadDateTimePicker expiresTimePicker = (RadDateTimePicker)item["Expires"].Controls[0];
expiresTimePicker.TimeView.Columns = 2;
 
GridDateTimeColumnEditor colEd = (item.EditManager.GetColumnEditor("Expires") as GridDateTimeColumnEditor);
(colEd.PickerControl as RadDateTimePicker).TimeView.Columns = 2;

The column is declared as: 
<telerik:GridDateTimeColumn DataField="Expires" DataFormatString="{0:g}" ReadOnly="false"  PickerType="DateTimePicker"
      HeaderText="Expires" UniqueName="Expires" SortExpression="Expires" AllowFiltering="false" ConvertEmptyStringToNull="true" >
</telerik:GridDateTimeColumn>


While debugging, these statements do get executed and the Columns value is updated to 2. When I test the result in the browser the TimeView still pops up with a 3 column layout - so the code appears to have had no effect.

Is the itemDataBound event the wrong event for such a change? I've tried to add similar code to the ItemCreated event but not been able to access the editor there at all. 


5 Answers, 1 is accepted

Sort by
0
Kostadin
Telerik team
answered on 29 Sep 2012, 10:39 PM
Hello Jonathan,

OnItemDataBound event handler is to early in the lifecycle of the page to change the number of columns. You could do it at OnPreRender event. Also you have to use SharedTimeView which is reference to the TimeView that will be used for picking time.
protected void RadGrid1_PreRender(object sender, EventArgs e)
{
    if (RadGrid1.EditIndexes.Count != 0)
    {
        int index = int.Parse(RadGrid1.EditIndexes[0]);
        GridEditFormItem editForm = RadGrid1.MasterTableView.GetItems(GridItemType.EditFormItem)[index] as GridEditFormItem;
        RadDateTimePicker expiresTimePicker = (RadDateTimePicker)editForm["BirthDate"].Controls[0];
        expiresTimePicker.SharedTimeView.Columns = 2;
        expiresTimePicker.TimeView.Columns = 2;
    }
}


Greetings,
Kostadin
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
0
Jonathan
Top achievements
Rank 1
answered on 01 Oct 2012, 05:32 PM
This code doesn't seem to be correct. I get an "index out of bounds" error at the line that starts with: GridEditFormItem editForm = RadGrid1... 

When debugging the index variable is non-zero.

And is the last line inside the if statement really necessary given that the line above it should be setting the number of columns?
0
Kostadin
Telerik team
answered on 02 Oct 2012, 01:59 PM
Hi Jonathan,

The last row is not necessary, thank you for pointing that out.
As for the error, I cannot tell for sure why you are getting this exception without your code declaration. For convenience I prepared a small sample where I used the same method to change the time view columns and attached it to this thread. 

I hope this proves helpful.

Regards,
Kostadin
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
0
Jonathan
Top achievements
Rank 1
answered on 02 Oct 2012, 05:17 PM
This was my column defintion:

<telerik:GridDateTimeColumn DataField="Expires" DataFormatString="{0:g}" ReadOnly="false"  PickerType="DateTimePicker"
      HeaderText="Expires" UniqueName="ExpiresTT" SortExpression="Expires" AllowFiltering="false" ConvertEmptyStringToNull="true" >
</telerik:GridDateTimeColumn>

My grid was set to use In-Place editing (<MasterTableView DataKeyNames="Id"  EditMode="InPlace"  ...)

And so I had to modify the code to the following to get it to work: (not sure if this is how the Telerik team would do it or not)

protected void RadGrid1_PreRender(object sender, EventArgs e)
{
    if (RadGrid1.EditIndexes.Count != 0)
    {
        // to access the In-Place editing TimePicker
        int index = 0; // int.Parse(RadGrid1.EditIndexes[0]);
        GridEditableItem editedItem = RadGrid1.MasterTableView.GetItems(GridItemType.EditItem)[index] as GridEditableItem;
        GridEditManager editMan = editedItem.EditManager;
        GridDateTimeColumnEditor expiresTimePicker = (GridDateTimeColumnEditor)editMan.GetColumnEditor("ExpiresTT");
        expiresTimePicker.SharedTimeView.Columns = 2;
    }
}

0
Accepted
Kostadin
Telerik team
answered on 03 Oct 2012, 02:41 PM
Hi Jonathan,

We would be doing it the same way. Here is the code snippet.
protected void RadGrid1_PreRender(object sender, EventArgs e)
    {
        if (RadGrid1.EditIndexes.Count != 0)
        {
            GridEditableItem editForm = RadGrid1.MasterTableView.GetItems(GridItemType.EditItem)[0] as GridEditableItem;
            RadDateTimePicker expiresTimePicker = (RadDateTimePicker)editForm["BirthDate"].Controls[0];
            expiresTimePicker.SharedTimeView.Columns = 2;
        
    }


Regards,
Kostadin
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
Tags
Grid
Asked by
Jonathan
Top achievements
Rank 1
Answers by
Kostadin
Telerik team
Jonathan
Top achievements
Rank 1
Share this question
or