This is a migrated thread and some comments may be shown as answers.

Radgrid BatchEdit and dynamically making fields ReadOnly

3 Answers 238 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Skip
Top achievements
Rank 1
Skip asked on 24 Mar 2016, 08:17 PM

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>

3 Answers, 1 is accepted

Sort by
0
Accepted
Viktor Tachev
Telerik team
answered on 29 Mar 2016, 06:01 AM
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.
0
Skip
Top achievements
Rank 1
answered on 13 Apr 2016, 08:30 PM

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

 

 

0
Viktor Tachev
Telerik team
answered on 14 Apr 2016, 09:50 AM
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.
Tags
Grid
Asked by
Skip
Top achievements
Rank 1
Answers by
Viktor Tachev
Telerik team
Skip
Top achievements
Rank 1
Share this question
or