Radgrid BatchEdit and dynamically making fields ReadOnly

4 posts, 1 answers
  1. Skip
    Skip avatar
    14 posts
    Member since:
    Mar 2013

    Posted 24 Mar Link to this post

    The radgrid below is a shorter version of the one I'm working on.  I need to make "CYF_UNITS_JAN" ReadOnly by code behind.

    Note the EditMode of the grid is Batch and EditType is Row.

    How do you access the ReadOnly="True" property in code behind while the grid is being edited this way?  I've tried a few methods but it doesn't seem to be working.

    <telerik:RadGrid ID="RadGrid1" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource1" AllowAutomaticUpdates="True">
        <MasterTableView DataKeyNames="ID" DataSourceID="SqlDataSource1" EditMode="Batch">
            <CommandItemSettings ShowAddNewRecordButton="False" ShowSaveChangesButton="True" />
            <BatchEditingSettings EditType="Row"/>
            <Columns>
                <telerik:GridBoundColumn DataField="ID" DataType="System.Int32" FilterControlAltText="Filter ID column" HeaderText="ID" ReadOnly="True" SortExpression="ID" UniqueName="ID" Visible="false">
                </telerik:GridBoundColumn>
                <telerik:GridBoundColumn DataField="SalesPersonNo" FilterControlAltText="Filter SalesPersonNo column" HeaderText="SalesPersonNo" SortExpression="SalesPersonNo" UniqueName="SalesPersonNo" Visible="false">
                </telerik:GridBoundColumn>
                <telerik:GridBoundColumn DataField="ItemCode" FilterControlAltText="Filter ItemCode column" HeaderText="Item Code" SortExpression="ItemCode" UniqueName="ItemCode" ReadOnly="true">
                </telerik:GridBoundColumn>
                <telerik:GridBoundColumn DataField="CYF_UNITS_JAN" DataType="System.Int32" FilterControlAltText="Filter CYF_UNITS_JAN column" HeaderText="CY JAN" SortExpression="CYF_UNITS_JAN" UniqueName="CYF_UNITS_JAN" DataFormatString="{0:n2}">
                </telerik:GridBoundColumn>
                <telerik:GridBoundColumn DataField="CYF_UNITS_FEB" DataType="System.Int32" FilterControlAltText="Filter CYF_UNITS_FEB column" HeaderText="FEB" SortExpression="CYF_UNITS_FEB" UniqueName="CYF_UNITS_FEB" DataFormatString="{0:n2}">
                </telerik:GridBoundColumn>
            </Columns>
        </MasterTableView>
    </telerik:RadGrid>

  2. Answer
    Viktor Tachev
    Admin
    Viktor Tachev avatar
    1488 posts

    Posted 29 Mar Link to this post

    Hello Skip,

    Since the RadGrid is defined declaratively it is recommended to set the ReadOnly property for the column in the markup:

    <telerik:GridBoundColumn DataField="CYF_UNITS_JAN" DataType="System.Int32" FilterControlAltText="Filter CYF_UNITS_JAN column" HeaderText="CY JAN"
        SortExpression="CYF_UNITS_JAN" UniqueName="CYF_UNITS_JAN" DataFormatString="{0:n2}" ReadOnly="true">
    </telerik:GridBoundColumn>


    Alternatively you can handle the PreRender event for RadGrid and set the column as ReadOnly in the handler.

    protected void RadGrid1_PreRender(object sender, EventArgs e)
    {
        (RadGrid1.MasterTableView.GetColumnSafe("CYF_UNITS_JAN") as GridBoundColumn).ReadOnly = true;
    }


    Regards,
    Viktor Tachev
    Telerik
    Do you need help with upgrading your ASP.NET AJAX, WPF or WinForms projects? Check the Telerik API Analyzer and share your thoughts.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Skip
    Skip avatar
    14 posts
    Member since:
    Mar 2013

    Posted 13 Apr in reply to Viktor Tachev Link to this post

    Since the month has to be set to read only automatically, this worked like a charm!  Code in VB.

    Protected Sub RadGrid1_PreRender(sender As Object, e As EventArgs)
        Dim CM As Int32 = Date.Now.Month
        If CM >= 1 Then
            TryCast(RadGrid1.MasterTableView.GetColumnSafe("CYF_UNITS_JAN"), GridBoundColumn).[ReadOnly] = True
        End If
        If CM >= 2 Then
            TryCast(RadGrid1.MasterTableView.GetColumnSafe("CYF_UNITS_FEB"), GridBoundColumn).[ReadOnly] = True
        End If
        If CM >= 3 Then
            TryCast(RadGrid1.MasterTableView.GetColumnSafe("CYF_UNITS_MAR"), GridBoundColumn).[ReadOnly] = True
        End If
        If CM >= 4 Then
            TryCast(RadGrid1.MasterTableView.GetColumnSafe("CYF_UNITS_APR"), GridBoundColumn).[ReadOnly] = True
        End If
        If CM >= 5 Then
            TryCast(RadGrid1.MasterTableView.GetColumnSafe("CYF_UNITS_MAY"), GridBoundColumn).[ReadOnly] = True
        End If
        If CM >= 6 Then
            TryCast(RadGrid1.MasterTableView.GetColumnSafe("CYF_UNITS_JUN"), GridBoundColumn).[ReadOnly] = True
        End If
        If CM >= 7 Then
            TryCast(RadGrid1.MasterTableView.GetColumnSafe("CYF_UNITS_JUL"), GridBoundColumn).[ReadOnly] = True
        End If
        If CM >= 8 Then
            TryCast(RadGrid1.MasterTableView.GetColumnSafe("CYF_UNITS_AUG"), GridBoundColumn).[ReadOnly] = True
        End If
        If CM >= 9 Then
            TryCast(RadGrid1.MasterTableView.GetColumnSafe("CYF_UNITS_SEP"), GridBoundColumn).[ReadOnly] = True
        End If
        If CM >= 10 Then
            TryCast(RadGrid1.MasterTableView.GetColumnSafe("CYF_UNITS_OCT"), GridBoundColumn).[ReadOnly] = True
        End If
        If CM >= 11 Then
            TryCast(RadGrid1.MasterTableView.GetColumnSafe("CYF_UNITS_NOV"), GridBoundColumn).[ReadOnly] = True
        End If
        If CM >= 12 Then
            TryCast(RadGrid1.MasterTableView.GetColumnSafe("CYF_UNITS_DEC"), GridBoundColumn).[ReadOnly] = True
        End If
     
    End Sub

     

     

  5. Viktor Tachev
    Admin
    Viktor Tachev avatar
    1488 posts

    Posted 14 Apr Link to this post

    Hi Skip,

    I am glad that the suggested approach is working for you. Thank you for sharing your VB code with the community.

    Regards,
    Viktor Tachev
    Telerik
    Do you need help with upgrading your ASP.NET AJAX, WPF or WinForms projects? Check the Telerik API Analyzer and share your thoughts.
Back to Top