Disable selection for some rows in GridClientSelectColumn conditionally

Thread is closed for posting
5 posts, 0 answers
  1. Telerik Admin
    Telerik Admin avatar
    1679 posts
    Member since:
    Oct 2004

    Posted 16 Apr 2007 Link to this post

    Requirements

    RadGrid for ASP .NET version

    RadControls for ASP .NET AJAX version

    Q4 2006 SP2 and later


    2008.1.415 and later
    .NET version

    2.0 and later
    Visual Studio version

    2005 and later
    Programming language

    C#, Javascrpt
    Browser support

    all supported by RadGrid for ASP .NET


    all browsers supported by RadControls for ASP .NET AJAX

    To convert code from posted projects Telerik online converter


    PROJECT DESCRIPTION


    In some particular cases you may need to disallow selection for part of the rows in grid having GridClientSelectColumn. Wiring the ItemCreated or ItemDataBound events and disabling the checkbox in that GridClientSelectColumn solves the problem partially. Still if the checkbox in the grid header is checked -- all grid items become selected.

    The attached project uses a hidden input to send the indexes of the non-selectable items to the client. On the client these values are loaded into an array. Then inside RowSelecting client event handler of the grid a javascript function is executed to check if the index of the row being selected is in the list. If that is true the event has been cancelled.

    Below is the essential part of the code:

    protected void RadGrid1_ItemDataBound(object sender, Telerik.WebControls.GridItemEventArgs e) 
        if (e.Item is GridDataItem) //Your condition goes here 
        { 
            ((e.Item as GridDataItem)["Select"].Controls[1] as CheckBox).Enabled = false
            Sel.Value += e.Item.ItemIndex.ToString() + ":"
        } 

        <script type="text/javascript"
        var a = Array; 
        window.onload = function () 
        { 
            var inp = document.getElementById('Sel'); 
            var data = inp.value; 
            if(data != ""
            { 
                var rowsData = data.split(":"); 
                var i=0; 
                while(typeof(rowsData[i]) != "undefined"
                { 
                    if (rowsData[i] != ""
                    { 
                        a[i] = rowsData[i]; 
                    } 
                    i++; 
                } 
            } 
        }     
        function Selecting (row)  
        { 
            var i = 0; 
            while(typeof(a[i]) != "undefined"
            { 
                if (a[i++] == row.Index) 
                { 
                    return false
                } 
            } 
        } 
        </script> 
     
  2. Telerik Admin
    Telerik Admin avatar
    1679 posts
    Member since:
    Oct 2004

    Posted 09 Jul 2007 Link to this post

    The additional literal control, part of GridClientSelectColumn's cells, has been removed since v4.6.2 of RadGrid. So the code above needs to be modified a bit:

        protected void RadGrid1_ItemDataBound(object sender, Telerik.WebControls.GridItemEventArgs e) 
        { 
            if (e.Item is GridDataItem &&  
               (e.Item.ItemIndex == 5 || e.Item.ItemIndex == 6 || e.Item.ItemIndex == 1))   //Your condition goes here 
            { 
                ((e.Item as GridDataItem)["Select"].Controls[0] as CheckBox).Enabled = false;  // first control since v4.6.2 
                Sel.Value += e.Item.ItemIndex.ToString() + ":"
            } 
        } 

    Attachment added into initial post.
  3. Telerik Admin
    Telerik Admin avatar
    1679 posts
    Member since:
    Oct 2004

    Posted 18 Feb 2008 Link to this post

    Hello,

    Attached to the initial post, you will find the same technique working with Telerik.Web.UI RadGrid. The JavaScript code is pretty much the same.
  4. Sam
    Sam avatar
    4 posts
    Member since:
    Aug 2011

    Posted 25 Aug 2011 Link to this post

    I can't get it working in version 2011.2.712.40

    Please provide a working solution .
  5. Tsvetina
    Admin
    Tsvetina avatar
    1876 posts

    Posted 25 Aug 2011 Link to this post

    Hi Sam,

    I tested the code files with RadControls version 2011.2.712 and I did not notice any problems.

    You can find the updated archive attached to this post.

    Greetings,
    Tsvetina
    the telerik team

    Instantly find answers to your questions at the new telerik Support Center
Back to Top