How to set default value for fields in code behind for grid insert?

2 posts, 1 answers
  1. Jerald
    Jerald avatar
    32 posts
    Member since:
    Jan 2016

    Posted 18 Sep 2018 Link to this post

    I have a grid that allows inserting and updating.  I would like to set the default value of three fields in the grid and not allow the user to change those values.  It would be even better if these fields weren't even visible.  The "ScoreGroupId" should get it's value from a hidden field titled "hdnScoreGroupId".  "ScoreCrtdBy" should be the current user - "HttpContext.Current.User.Identity.Name".  And "ScoreCrtdDt" is the current date and time and should use "DateTime.Now.ToString()".  How can I do this for the following grid:

    <telerik:RadGrid ID="rgScores" runat="server" Skin="Office2010Black" DataSourceID="sdsRgScores" AutoGenerateColumns="False">
         <MasterTableView DataKeyNames="Id" DataSourceID="sdsRgScores" CommandItemDisplay="Top" InsertItemPageIndexAction="ShowItemOnCurrentPage">
              <Columns>
                   <telerik:GridEditCommandColumn></telerik:GridEditCommandColumn>
                   <telerik:GridBoundColumn DataField="Id" ReadOnly="True" HeaderText="Id" SortExpression="Id" UniqueName="Id" DataType="System.Int32" FilterControlAltText="Filter Id column"></telerik:GridBoundColumn>
                   <telerik:GridBoundColumn DataField="ScoreGroupId" HeaderText="ScoreGroupId" SortExpression="ScoreGroupId" UniqueName="ScoreGroupId" DataType="System.Int32" FilterControlAltText="Filter ScoreGroupId column"></telerik:GridBoundColumn>
                   <telerik:GridBoundColumn DataField="ScoreCrtdBy" HeaderText="ScoreCrtdBy" SortExpression="ScoreCrtdBy" UniqueName="ScoreCrtdBy" FilterControlAltText="Filter ScoreCrtdBy column"></telerik:GridBoundColumn>
                   <telerik:GridBoundColumn DataField="ScoreCrtdDt" HeaderText="ScoreCrtdDt" SortExpression="ScoreCrtdDt" UniqueName="ScoreCrtdDt" DataType="System.DateTime" FilterControlAltText="Filter ScoreCrtdDt column"></telerik:GridBoundColumn>
                   <telerik:GridBoundColumn DataField="Score" HeaderText="Score" SortExpression="Score" UniqueName="Score" DataType="System.Int32" FilterControlAltText="Filter Score column"></telerik:GridBoundColumn>             
              </Columns>
         </MasterTableView>
    </telerik:RadGrid>
  2. Answer
    Tsvetomir
    Admin
    Tsvetomir avatar
    197 posts

    Posted 20 Sep 2018 Link to this post

    Hi Jerald,

    The first and most straightforward approach would be to take advantage of the DefaultInsertValue property of the controls, for example:
    <telerik:GridDateTimeColumn ... DefaultInsertValue="5/5/2015">

    You can, also, access the controls manually in edit mode and set their ReadOnly property to true. You can do this in the ItemDataBound event handler. You can refer to the following code snippet:

    protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
    {
        if (e.Item is GridEditableItem && e.Item.IsInEditMode)
        {
            GridEditableItem editableItem = e.Item as GridEditableItem;
            TextBox textBox = editableItem["ColumnName"].Controls[0] as TextBox;
            textBox.ReadOnly = true;
        }
    }

    Another alternative would be to hide the item from the insert/edit form. You can access the control and set its visible property to false. Note that the control rendered on the page is inside a <td> tag, which is inside a <tr> tag. In order to fully hide the item from the form, you will have the hide its Parent elements as well:

    protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
    {
        if (e.Item is GridEditableItem && e.Item.IsInEditMode)
        {
            GridEditableItem editableItem = e.Item as GridEditableItem;
            TextBox textBox = editableItem["ColumnName"].Controls[0] as TextBox;
            textBox.Visible = false;
            textBox.Parent.Visible = false;
        }
    }

    You can find extensive explanation on how to access the controls here: Accessing Raw Field Data and Key Values. Furthermore, if you would like to execute different logic for the different modes - Insert mode and Edit mode,  you can check out the Distinguish Edit or Insert Mode help article.

    Kind regards,
    Tsvetomir
    Progress Telerik
    Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
Back to Top