【问题标题】:Telerik RadGrid - How do I set default data on insert?Telerik RadGrid - 如何在插入时设置默认数据?
【发布时间】:2013-01-18 19:32:06
【问题描述】:

当我单击添加记录按钮时,我希望我的其中一列具有默认值。我如何在后面的代码中做到这一点?这是一个动态的日期,并且可以随时更改?

【问题讨论】:

    标签: telerik radgrid


    【解决方案1】:

    如果列不是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
    

    【讨论】:

      【解决方案2】:

      可以拦截ItemDataBound事件,并渲染内容进行控制。

      <telerik:RadGrid ID="RadGrid1" AutoGenerateColumns="false" 
          AllowSorting="True" runat="server" OnNeedDataSource="RadGrid1_NeedDataSource" 
          OnItemDataBound="RadGrid1_ItemDataBound">
          <MasterTableView DataKeyNames="ID" CommandItemDisplay="Top">
              <Columns>
                  <telerik:GridBoundColumn DataField="ID" HeaderText="ID" 
                      UniqueName="ID" />
                  <telerik:GridBoundColumn DataField="Name" HeaderText="Name" 
                      UniqueName="Name" />
              </Columns>
              <EditFormSettings ColumnNumber="1" EditFormType="Template">
                  <FormTemplate>
                      Name:<asp:TextBox runat="server" ID="NameTextBox"/>
                  </FormTemplate>
              </EditFormSettings>
          </MasterTableView>
      </telerik:RadGrid>
      
      public class Customer
      {
          public int ID { get; set; }
          public string Name { get; set; }
      }
      
      protected void RadGrid1_NeedDataSource(object sender, 
          GridNeedDataSourceEventArgs e)
      {
          RadGrid1.DataSource = new List<Customer>
              {
                  new Customer {ID = 1, Name = "John"},
                  new Customer {ID = 2, Name = "Marry"},
                  new Customer {ID = 3, Name = "Eric"}
              };
      }
      
      protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
      {
          if (e.Item is GridEditFormItem && e.Item.IsInEditMode)
          {
              var item = e.Item as GridEditFormItem;
      
              var nameTextBox = item.FindControl("NameTextBox") as TextBox;
      
              // Insert mode
              if (e.Item.OwnerTableView.IsItemInserted)
              {
                  nameTextBox.Text = "Please enter name";
              }
              else // Edit mode
              {
      
              }
          }
      

      【讨论】:

        猜你喜欢
        • 2011-05-21
        • 2010-10-15
        • 1970-01-01
        • 2019-03-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多