making rad grid Inidividual cell read only based ona condition.

3 posts, 1 answers
  1. Venkatesh
    Venkatesh avatar
    21 posts
    Member since:
    Jul 2014

    Posted 14 Aug 2014 Link to this post

    Hi,



    I want to make read only for the rad grid cells based on a condition.

     

    Eg:  1      2     3      N/A    5        6      7     N/A

            23   34   56    12      N/A    12     10   33



    The cells which is having N/A i need to make read only.



    I am using radgrid batch edit with GridTemplateColumn. Label in ItemTemplate, RadNumericTextBox  in EditItemTemplate.





    Regards,

    Venkatesh.
  2. Answer
    Konstantin Dikov
    Admin
    Konstantin Dikov avatar
    1803 posts

    Posted 18 Aug 2014 Link to this post

    Hello Venkatesh,

    For achieving such behavior, which simulates a read only cell when some condition is met, you should handle the client-side OnBatchEditOpenening event of the grid, retrieve the current cell value through the Batch Editing Manager getCellValue(cell) method and cancel the event (if the value is "N/A" for your scenario).

    For your convenience, following is a simple example demonstrating the above approach. Please give it a try and see if it meets you exact requirement:
    <telerik:RadCodeBlock ID="RadCodeBlock1" runat="server">
        <script type="text/javascript">
            function batchEditOpening(sender, args) {
                var batchManager = sender.get_batchEditingManager();
                var cellValue = batchManager.getCellValue(args.get_cell());
                if (cellValue == "N/A") {
                    args.set_cancel(true);
                }
            }
        </script>
    </telerik:RadCodeBlock>
     
     
    <telerik:RadGrid ID="RadGrid1" OnNeedDataSource="RadGrid1_NeedDataSource" OnItemDataBound="RadGrid1_ItemDataBound"     
        runat="server">
        <MasterTableView AutoGenerateColumns="false" EditMode="Batch">
            <Columns>
                <telerik:GridTemplateColumn UniqueName="Value1">
                    <ItemTemplate>
                        <asp:Label Text='<%#Eval("Value1") %>' runat="server" />
                    </ItemTemplate>
                    <EditItemTemplate>
                        <telerik:RadNumericTextBox runat="server"></telerik:RadNumericTextBox>
                    </EditItemTemplate>
                </telerik:GridTemplateColumn>                  
            </Columns>
        </MasterTableView>
        <ClientSettings>
            <ClientEvents OnBatchEditOpening="batchEditOpening" />
        </ClientSettings>
    </telerik:RadGrid>

    And the dummy data:
    protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
    {
        DataTable table = new DataTable();
        table.Columns.Add("ID", typeof(int));
        table.Columns.Add("Value1", typeof(Decimal));
        for (int i = 0; i < 10; i++)
        {
            table.Rows.Add(i, i);
            table.Rows.Add(i);
        }
     
        (sender as RadGrid).DataSource = table;
     
    }
     
    protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
    {
        if (e.Item is GridDataItem)
        {
            GridDataItem item = e.Item as GridDataItem;
            if (string.IsNullOrEmpty((item["Value1"].Controls[1] as Label).Text))
            {
                item["Value1"].Text = "N/A";
            }
        }
    }

    Hope this helps.


    Regards,
    Konstantin Dikov
    Telerik
     

    Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

     
  3. Venkatesh
    Venkatesh avatar
    21 posts
    Member since:
    Jul 2014

    Posted 18 Aug 2014 Link to this post

    Hi Konstantin,

    Thank you very much for the solution. Its working properly like what i need.

    Regards,
    Venkatesh.
Back to Top