12 Answers, 1 is accepted
You can set it in the markup like this:
<
telerik:GridDateTimeColumn
DefaultInsertValue
=
"Today"
></
telerik:GridDateTimeColumn
>
Regards,
Maria Ilieva
Telerik
Thank you for the response Maria. Unfortunately, this does not work on my end. When I click the button in the grid to add a new record, the GridDateTimeColumn simply displays the text "Today" in the cell.
I thought that I might have to set this value in one of the client side events such as 'OnBatchEditOpening'. However, I'm not sure how to distinguish between insert and edit mode in this event, and also I'm not sure how to get a reference to the RadDateTimePicker in the GridDateTimeColumn to set Today's date.
Amy
Try the following code snippet in order to set current date to DatePicker when in Insert mode.
protected
void
RadGrid1_ItemDataBound(
object
sender, GridItemEventArgs e)
{
if
(e.Item
is
GridDataInsertItem && e.Item.OwnerTableView.IsItemInserted)
{
GridDataInsertItem insertItem = (GridDataInsertItem)e.Item;
(insertItem[
"DateTimeColumn_UniqueName"
].Controls[0]
as
RadDatePicker).SelectedDate = DateTime.Today;
}
}
Regards,
Maria Ilieva
Telerik
Hello Maria,
From what I understand, a server-side event like ItemDataBound ​is not triggered because Batch Edit is a client-side oriented mode and the entire editing is handled on the client-side. Thus I don't believe your suggestion above will work.
Can you please provide some guidance on how to set the default insert value to today for a GridDateTimeColumn in one of the client-side events?
Much appreciated.
Amy
With batch editing you can use the following:
protected
void
RadGrid1_PreRender(
object
sender, EventArgs e)
{
GridTableView masterTable = ((RadGrid)sender).MasterTableView;
GridDateTimeColumnEditor editor = masterTable.GetBatchColumnEditor(
"OrderDate"
)
as
GridDateTimeColumnEditor;
RadDatePicker picker = editor.PickerControl;
picker.SelectedDate = DateTime.Today;
}
Regards,
Maria Ilieva
Telerik
Thank you for the suggestion, but this is not working on my end. Is it possible for you to provide a runnable example where this is working? Or, suggest a way to do this in one of the client-side events?
Thank you,
Amy
It appears that we have an issue with setting default insert value when batch editing is enabled and this issue is logged in our system for further investigation.
Meanwhile you can handle the client OnRowCreated event and apply the default date like this:
function
RowCreated(sender, args) {
if
(args.get_itemIndexHierarchical() < 0) {
args.get_tableView().get_dataItems();
var
cell = args.get_tableView().getCellByColumnUniqueName(args.get_gridDataItem(),
"HireDate"
);
args.get_tableView().get_owner().get_batchEditingManager().changeCellValue(cell,
new
Date().toString());
}
}
I hope this helps.
Regards,
Maria Ilieva
Telerik
<
telerik:RadGrid
ID
=
"RadGrid1"
runat
=
"server"
AllowAutomaticInserts
=
"true"
DataSourceID
=
"SqlDataSource1"
>
<
MasterTableView
EditMode
=
"Batch"
DataKeyNames
=
"EmployeeID"
CommandItemDisplay
=
"Top"
>
<
Columns
>
<
telerik:GridDateTimeColumn
UniqueName
=
"BirthDateColumn"
DataField
=
"BirthDate"
></
telerik:GridDateTimeColumn
>
<
telerik:GridTemplateColumn
Display
=
"false"
>
<
ItemTemplate
>
<%(RadGrid1.MasterTableView.GetColumn("BirthDateColumn") as GridEditableColumn).DefaultInsertValue = DateTime.Now.ToString();%>
</
ItemTemplate
>
</
telerik:GridTemplateColumn
>
</
Columns
>
</
MasterTableView
>
</
telerik:RadGrid
>
Regards,
Edward
Thank you Maria and Edward for your suggestions. Please see the results of my test below:
- Maria's suggestion using the OnRowCreated event.
- Sets the initial date with the proper formatting.
- However, the date is not saved when the Save button is clicked. Note that I am saving the grid data using the method as described here - http://www.telerik.com/forums/how-do-i-save-2-radgrids-by-only-1-outside-button.
-
Edward's suggestion using an additional hidden column.
- Sets the initial date, but does not have the proper formatting.- The date is not saved when the Save button is clicked.
Almost have a working solution... But is seems that the BatchEditingManager doesn't think that there are table changes when setting the initial date this way.
After fixing the bug below you will be able to achieve the required functionality:
http://feedback.telerik.com/Project/108/Feedback/Details/172538-setting-defaultinsertvalue-does-not-work-for-a-batch-editing-grid
We increase its priority and I hope that the fix will be available in the next official release of the controls.
Regards,
Maria Ilieva
Telerik
This issue is already fixed. You can upgrade your applictaion to test the fix locally and let us know if it works fr you.
Regards,
Maria Ilieva
Telerik