当我单击“添加记录”按钮时,我希望其中一列具有默认值。我如何在后面的代码中做到这一点?这是一个动态日期并且可以随时更改?
最佳答案
如果该列不是 GridTemplateColumn
,您可以使用该列的 DefaultInsertValue
属性指定默认值,如下所示:
<telerik:GridBoundColumn DefaultInsertValue="12/21/2012" DataType="System.DateTime" DataField="Column1" UniqueName="Column1"></telerik:GridBoundColumn>
否则,如果它是 GridTemplateColumn
,请查看以下 Telerik 文章:
Inserting Values Using InPlace and EditForms Modes
更新:
您还可以在代码隐藏中使用 ItemCommand
方法指定默认列值:
Protected Sub RadGrid1_ItemCommand(ByVal source As Object, ByVal e As Telerik.Web.UI.GridCommandEventArgs) Handles RadGrid1.ItemCommand
If (e.CommandName = RadGrid.InitInsertCommandName) Then
'cancel the default operation
e.Canceled = True
'Prepare an IDictionary with the predefined values
Dim newValues As System.Collections.Specialized.ListDictionary = New System.Collections.Specialized.ListDictionary()
newValues("Column1") = New DateTime(2013, 1, 22)
newValues("Column2") = "hello"
newValues("Column3") = Nothing
'Insert the item and rebind
e.Item.OwnerTableView.InsertItem(newValues)
End If
End Sub
关于Telerik RadGrid - 如何设置插入时的默认数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14406276/