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

RadGrid GridClientSelectColumn Highlights rows that have disabled checkboxes

8 Answers 290 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Mike
Top achievements
Rank 1
Mike asked on 28 Mar 2013, 01:36 PM
I have a Radgrid that has AllowMultiRowSelection="True". However in my code behind I have logic to disable and hide certain rows.

In RadGrid1_ItemDataBound I have this snippet of code to disable and hide rows that should not be checked.
((e.Item as GridDataItem)["ClientSelectColumn"].Controls[0] as CheckBox).Enabled = false;
((e.Item as GridDataItem)["ClientSelectColumn"].Controls[0] as CheckBox).Visible = false;

The issue I am having is that even though the checkbox is not enabled and hidden for certain rows when I click on the checkbox in the header of the RadGrid, even the rows in the RadGrid that don't have a checkbox are highlighted. 

I only want the rows in the RadGrid to highlight the ones with checkboxes when a user selects the checkbox in the header and not the ones that I have hidden the checkbox.  What is the best and most practical way to achieve this scenario?

8 Answers, 1 is accepted

Sort by
0
Accepted
Princy
Top achievements
Rank 2
answered on 01 Apr 2013, 12:50 PM
Hi,

Please try the following code snippet.

ASPX:
<telerik:RadGrid>
   . . . . . . . . . . . . . . . . . .
    <ClientSettings>
       <ClientEvents OnRowSelecting="RowSelecting"/>
       <Selecting AllowRowSelect="true" />
    </ClientSettings>
  
       . . . . . . . . . . . . . . . . . . .
 </telerik:RadGrid>
   
<asp:HiddenField ID="HiddenField1" runat="server" />
<asp:HiddenField ID="HiddenField2" runat="server" />

C#:
protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e)
{
    if (e.Item is GridHeaderItem)
    {
        GridHeaderItem headerItem = (GridHeaderItem)e.Item;
        CheckBox chk = (CheckBox)headerItem["ClientSelectColumn"].Controls[0];
        HiddenField1.Value = chk.ClientID;
    }
}

Javascript:
<script type="text/javascript">
    var selecting;
    var grid;
    var checkBox;
    function pageLoad() {
        checkBox = $get($get('<%= HiddenField1.ClientID %>').value)
        $addHandler(checkBox, "click", onclickHandler);
        grid = $find('<%=RadGrid1.ClientID %>')
        var tmp = grid.get_masterTableView().get_dataItems().length;
        if (parseInt($get('<%= HiddenField2.ClientID %>').value, 10) + grid.get_masterTableView().get_selectedItems().length ==             grid.get_masterTableView().get_dataItems().length) {
            checkBox.checked = true;
        }
    }
    function onclickHandler() {
        if (grid.get_masterTableView().get_selectedItems().length > 0) {
            checkBox.checked = true;
        }
        else {
            checkBox.checked = false;
        }
    }
    var selecting = true;
    function RowSelecting(sender, args) {
        var id = args.get_id();
        var inputCheckBox = $get(id).getElementsByTagName("input")[0];
        if (!inputCheckBox || inputCheckBox.disabled) {
            //cancel selection for disabled rows
            args.set_cancel(true);
        }
        // if no more unselected enabled rows left - check the header checkbox
        else if (parseInt($get('<%= HiddenField2.ClientID %>').value, 10) + grid.get_masterTableView().get_selectedItems().length + 1 ==                  grid.get_masterTableView().get_dataItems().length) {
            checkBox.checked = true;
        }
    }
</script>

Thanks,
Princy.
0
Mike
Top achievements
Rank 1
answered on 23 Apr 2013, 01:46 PM
As always, thank you again Princy!
0
Kent
Top achievements
Rank 1
answered on 20 Aug 2013, 05:55 PM

 

Im trying to do something similar but want to just hide the header check all checkbox.  I am receiving Unable to cast object of type 'System.Web.UI.LiteralControl' to type 'System.Web.UI.WebControls.CheckBox'

 

 

Private Sub RadGrid1_ItemDataBound(sender As Object, e As GridItemEventArgs) Handles RadGrid1.ItemDataBound
        If Me.DetailType = "PostalStreetMatch" AndAlso TypeOf e.Item Is GridHeaderItem Then
            Dim header As GridHeaderItem = DirectCast(e.Item, GridHeaderItem)
            Dim chkbox As CheckBox = DirectCast(header("ClientSelectColumn").Controls(0), CheckBox)
            chkbox.Visible = False
        End If
    End Sub
0
Mike
Top achievements
Rank 1
answered on 20 Aug 2013, 05:58 PM

You can hide the check all in the header with this code (c#)

// Hide the checkbox in the header to prevent a select all option in your ItemDataBound
if (e.Item is GridHeaderItem)
{
    CheckBox chkbx = (CheckBox)(e.Item as GridHeaderItem)["ClientSelectColumn"].Controls[0];
    //to disable the chekbox  
    chkbx.Enabled = false;
    //or to hide the checkbox  
    chkbx.Visible = false;
}
0
Kent
Top achievements
Rank 1
answered on 20 Aug 2013, 07:44 PM
Yes.  So, why is what im doing in VB not working the same?
0
Kent
Top achievements
Rank 1
answered on 20 Aug 2013, 08:19 PM
Nevermind.  I got it.  For some reason the first time the grid is created the checkbox isn't present and I just needed to check for its existence.

Private Sub RadGrid1_ItemDataBound(sender As Object, e As GridItemEventArgs) Handles RadGrid1.ItemDataBound
        If Me.DetailType = "PostalStreetMatch" AndAlso TypeOf e.Item Is GridHeaderItem Then
            Dim chkbox As CheckBox = TryCast(DirectCast(e.Item, GridHeaderItem)("ClientSelectColumn").Controls(0), CheckBox)
            If Not chkbox Is Nothing Then
                chkbox.Visible = False
            End If
 
        End If
    End Sub
0
Mike
Top achievements
Rank 1
answered on 20 Aug 2013, 08:34 PM
So, are you good now?
0
Albert Shenker
Top achievements
Rank 1
Veteran
Iron
answered on 07 Jul 2020, 05:48 PM
If you don't need to both select and disable a particular row at the same time, then setting  e.Item.SelectableMode = GridItemSelectableMode.None in the ItemDatabound event seems to do the trick with far less code. Set this property and also disable/hide the checkbox in the appropriate row and the "Select All" checkbox will ignore the row.
Tags
Grid
Asked by
Mike
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Mike
Top achievements
Rank 1
Kent
Top achievements
Rank 1
Albert Shenker
Top achievements
Rank 1
Veteran
Iron
Share this question
or