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

How to allow conditional row selection?

8 Answers 518 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Albert
Top achievements
Rank 1
Albert asked on 17 Aug 2010, 11:05 PM
Hello,
I have a grid with GridClientSelectColumn column and client setting with AllowRowSelect="true". Furthermore, on

ItemDataBound

event, I hide the checkbox based on some logic, because I want only specific rows to be selected. The problem is that even if I hide the checkbox, the grid allows the row to be selected by simply clicking anywhere on the row. What is a proper way to setup a grid that allows only specific rows to be selectable?

Thank you.

 

 

 

 

8 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 18 Aug 2010, 05:41 AM
Hello Albert,


You can easily achieve this from client side by attaching 'OnRowSelecting' client event and check for the condition. Now cancel the event using "eventArgs.set_cancel(true)" method based on condition.


If your condition is based on the cell value, then the following link will help you in getting the cell value for selected rows from client side.
Getting cell values for selected rows client side


-Shinu.
0
David Louth
Top achievements
Rank 1
answered on 22 Oct 2010, 04:29 PM
I have the same problem. Caturing the event and calling set_cancel works. However, if any other visible checkbox is selected, clicking anywhere on this row causes all other checkboxes to be de-selected. This happens before the OnRowSelecting event fires.

I have UseClientSelectColumnOnly="true", that only appears to matter if the checkbox is visible. I don't have the option of showing these as disabled but I also don't want any select activity on the rows where the checkbox is hidden. Is there a way to handle this?

Thanks, 
0
Veli
Telerik team
answered on 26 Oct 2010, 04:25 PM
Hi David,

You need to use a combination of the 3 approaches:

1. Sett UseClientSelectColumnOnly="true"
2. Hide the select checkbox
3. Prevent selection by setting args.set_cancel(true) in OnRowSelection.

However, for point 2, you need to set the "display:none" style of the select checkbox, not Visible="false". Setting VIsible="false" will prevent the checkbox from rendering, but RadGrid's selection logic depends on being able to find the select checkbox, so we need to keep it rendered. Therefore, you need to set its display:false style, so that it is not visible:

protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e)
{
    if (e.Item is GridDataItem)
    {
        if (e.Item.ItemIndex % 3 == 0) //or your business logic to determine non-selectable items
        {
            CheckBox selectCheckBox = ((GridDataItem)e.Item)["SelectColumn"].Controls[0] as CheckBox;
            selectCheckBox.Style["display"] = "none";
        }
    }
}

The combination of the above 3 steps allows you to select all items but those prevented from getting select and, additionally, prevents clicking on these items to deselect the previously selected items.

Veli
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
David Louth
Top achievements
Rank 1
answered on 26 Oct 2010, 05:23 PM
Ah, I was setting Visible to false. Style["display"] = "none" was the kicker. I then had check for this condition Style["display"] == "none" and skip them when acting on the grid's SelectedItems

Thanks
0
Chuck Jabbour
Top achievements
Rank 1
answered on 13 Feb 2012, 06:11 PM
I'm fine with all of this and my designated rows are no longer selectable. The problem now lies with the GridClientSelectColumn's header checkbox. You can check it to select all "selectable" rows and that works fine, but it knows that all actual rows are not selected. The header checkbox remains unchecked because the "non-selectable" rows are of course not selected. Checking the header checkbox again does not de-select any rows. The header checkbox never takes on a state of checked and therefore never unchecks.

I'm using version 2011.1.519.40

Here's what I did:
  • Created a js function to be called on the grid's OnRowSelecting event.
  • In OnItemDataBound of grid, disabled the checkbox on designated rows and set an attribute on a cell indicating it was not selectable
  • My js function (on row selecting) checks for that attribute and calls eventArgs.set_cancel(true) like this:
  • if ($(cell).data("sel") === "0") { eventArgs.set_cancel(true) }

0
Veli
Telerik team
answered on 14 Feb 2012, 11:18 AM
Hi Chuck,

Your observations are correct. The header checkbox determines that not all possible select checkboxes are  checked, therefore, not all items must be selected. Therefore, it doesn't toggle the checked state of the header checkbox. You cannot easily work around this behavior, because the checkboxes in the GridClientSelectColumn are tightly coupled with the selection internals in RadGrid. Currently, your best option is to use a custom template column with a checkbox control and implement selection programmatically, where some items may not participate in selection.

Veli
the Telerik team
Sharpen your .NET Ninja skills! Attend Q1 webinar week and get a chance to win a license! Book your seat now >>
0
jenny
Top achievements
Rank 1
answered on 24 Jul 2012, 09:09 AM
hi,

I need help and i dont know where to fix it
the problem is that on the first load of the page the check all items it does not check all the items but when you click the next page
and go back to the previous page when you slect to check all items it works it check all the items.

and the other problem is that some of the question items selected are repeated.

here is my aspx code
<telerik:RadGrid ID="ExportReponse_RadGrid" Width="97%"  AutoGenerateColumns="false" runat="server" 
     AllowSorting="True" PageSize="10" AllowPaging="True" AllowMultiRowSelection="True" Gridlines="None">
             <MasterTableView Width="100%" Summary="RadGrid table" />    
           <PagerStyle Mode="NextPrevAndNumeric"/>
             <ClientSettings EnableRowHoverStyle="true">
            <Selecting AllowRowSelect="true" />
        </ClientSettings>
        <SelectedItemStyle CssClass="SelectedItem" />
  <MasterTableView>
            <Columns> 
                    <telerik:GridClientSelectColumn UniqueName="CheckboxSelectColumn">
                    </telerik:GridClientSelectColumn>
                    <telerik:GridBoundColumn  UniqueName="ItemQuestionaire" DataField="item_text" DataType="System.String" HeaderText="Question Type"/> 
            </Columns>
   </MasterTableView>
</telerik:RadGrid> 


aspx.cs code
 LoadItems();
            ExportReponse_RadGrid.DataSource = null;
            ExportReponse_RadGrid.DataBind();
   
instead of clear i just put that code because in telerik it has no clear

Please help me thanks!
0
Eyup
Telerik team
answered on 26 Jul 2012, 03:08 PM
Hello Jenny,

I have created a sample RadGrid web site using the provided code. On my side selecting all of the items on initial load works as expected. Please find the attached application and try to distinguish the crucial differences between our projects.

In addition, please keep in mind that we suggest using of a more advanced binding approach in contrast to the simple DataBind() way:
Declarative DataSource
Advanced Data Binding
Advanced Data Binding Demo

All the best,
Eyup
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
Tags
Grid
Asked by
Albert
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
David Louth
Top achievements
Rank 1
Veli
Telerik team
Chuck Jabbour
Top achievements
Rank 1
jenny
Top achievements
Rank 1
Eyup
Telerik team
Share this question
or