Search code examples
asp.netteleriktelerik-gridradgrid

RadGrid make field invisible on Edit


<telerik:RadGrid runat="server" ID="rdReport" AutoGenerateColumns="false" AllowPaging="true" Skin="Metro"  OnItemCommand="ItemCommand" OnItemDataBound="rdReport_ItemDataBound" OnPreRender="rdReport_PreRender" DataSourceID="FountainSource" AllowAutomaticUpdates="true" AllowAutomaticDeletes="true">
    <MasterTableView DataKeyNames="ID" CommandItemDisplay="None">
        <Columns>
            <telerik:GridEditCommandColumn ButtonType="ImageButton" />                                   
            <telerik:GridBoundColumn DataField="LocName" HeaderText="Location" ReadOnly="true" /> 
            <     
            <telerik:GridBoundColumn DataField="Field1Value" HeaderText="Custom Field1" />
            <telerik:GridBoundColumn DataField="Field2Value" HeaderText="Custom Field2" />
            <telerik:GridBoundColumn DataField="Field3Value" HeaderText="Custom Field3" />

            <telerik:GridButtonColumn ConfirmText="Delete?" ConfirmDialogType="RadWindow"
                ConfirmTitle="Delete" ButtonType="ImageButton" CommandName="Delete" />
        </Columns>
        <EditFormSettings>
            <EditColumn ButtonType="ImageButton" />
        </EditFormSettings>
    </MasterTableView>
    <PagerStyle Mode="NextPrevAndNumeric" />
</telerik:RadGrid>

On Edit, I like to make a field invisible. I am using the following code which works but want to check to see if it is best practice:

protected void rdReport_ItemDataBound(object sender, GridItemEventArgs e)
{
    // Edit Mode
    if ((e.Item is GridEditFormItem) && (e.Item.IsInEditMode))
    {
        GridEditFormItem fndColumn = (GridEditFormItem)e.Item;
        fnColumn["Field1Value"].Parent.Visible = false;
    }
}

Solution

  • The solution which was provided by you is perfect but it would be nice if you will also add UniqueName property in each column. If we will not assign the UniqueName than it is consider DataField value as UniqueName.

    ASPX

    <telerik:GridButtonColumn DataField="Field1Value" HeaderText="Custom Field1" UniqueName="Field1Value" />
    

    ASPX.CS

    protected void rdReport_ItemDataBound(object sender, GridItemEventArgs e)
    {
        // Edit Mode
        if ((e.Item is GridEditFormItem) && (e.Item.IsInEditMode))
        {
            GridEditFormItem fndColumn = (GridEditFormItem)e.Item;
            fnColumn["Field1Value"].Parent.Visible = false; // "Field1Value" is column uniquename
        }
    }
    

    Let me know if you required more information.