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

What event to use in a gridview?

4 Answers 63 Views
GridView
This is a migrated thread and some comments may be shown as answers.
John
Top achievements
Rank 1
John asked on 28 Jan 2011, 02:09 AM
Greetings,

I have one situation that I don't really know what event should be used as followed:

I have one gridview that binds to a collection of users.  Each row displays User_ID, Last_name, First_Name, Role_ID, Is_Active (checkbox), EmailAddress, note and so on.....

When a user is trying to modify the Is_Active (checkbox) field in that gridview, I would like to check the Role_ID, if Role_ID = 2 then Is_Active checkbox will be disabled, not letting the user to change the value. 
If Role_ID = 1, then nothing happens.

What event should I used?  I am unable to find a right event for this validation.

Thanks

John.

4 Answers, 1 is accepted

Sort by
0
Maya
Telerik team
answered on 28 Jan 2011, 08:58 AM
Hi John,

You may take advantage of the IsReadOnlyBinding property of the column and bind it to an IValueConverter. Once the value of the Role_ID is 2, then you may return a "false" from the converter. Thus automatically, the CheckBox on that particular row will be marked as read-only. For example:

public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            var player = value as Player;
            return player.Number == 1 ? true : false;
        }

<telerik:GridViewDataColumn DataMemberBinding="{Binding IsActive}" IsReadOnlyBinding="{Binding Converter={StaticResource IntegerToBooleanConverter}}" />
 
I am sending you a sample project illustrating the proposed solution.
Another possible approach may be to expose a new property in you business object that will be bound again to the IsReadOnlyBinding of the column. In this case you may handle the CellEditEnded event and once the value of the Role_ID is 2, to set that particular property to false.


Greetings,
Maya
the Telerik team
Let us know about your Windows Phone 7 application built with RadControls and we will help you promote it. Learn more>>
0
John
Top achievements
Rank 1
answered on 28 Jan 2011, 02:06 PM
Dear Maya,
Thank you for your promptly response. I have studied your sample codes.  Unfortunately, it doesn't get close to my needs.

I am able to convert it to checkbox from integer (1 and 0) without any problem.  The data in my gridview displays correctly (Last Name, First Name, checkbox for Is_Active, Role, Role_ID, Email address, and so on...)

Your codes don't address the event that when a user clicks on a specific row in edit-mode, to make some change to any cell within that row, I would need to check the condition of the "Is_Active" column.

If the Role_ID = 2, then the Is_Active checkbox is now disabled => not letting the user to make any change to it.
If the Role_ID <> 2, then the Is_Active checkbox is enabled and a user can check/uncheck to that checkbox

Your sample codes show me how to convert from integer (1 or 0) into checkbox using IValueConverter. 

I hope I explain clear enough so you can capture my question.

Again, your support is truly appreciated.
Bests
John.



0
Accepted
Maya
Telerik team
answered on 28 Jan 2011, 02:31 PM
Hi John,

Please take another look at the same provided or the code-snippets above. The IValueConverter is not used for converting an integer value into a CheckBox. The CheckBox in the column is bound to the boolean property IsActive. Consequently, with or without the converter the GridViewDataColumn will still be accepted as a CheckBoxColumn.
The idea of the converter is to get a property from the item - in the sample - Number, in your project it will be Role_ID - and depending on its value (in your case 1 or 2 ), it will return true or false. The returned boolean value will be then bound to the IsReadOnlyBinding property that also accepts boolean values. So, once the returned from the converter value is true, that particular cell will become read-only. Once you change the value of the Number (Role_ID in your case), the converter will be again invoked and a true/ false value will be returned. Based on that value, the CheckBox will either still stay as read-only or become editable. 
However, if this does not correspond to your needs, you may handle CellEditEnded and verify the value of the Role_ID and disable the CheckBox in that item. Another possible approach may be to handle the BeginningEdit event and cancel it in case the value of the Role_ID is the one that requires the CheckBox to be read-only.
 

Best wishes,
Maya
the Telerik team
Let us know about your Windows Phone 7 application built with RadControls and we will help you promote it. Learn more>>
0
John
Top achievements
Rank 1
answered on 28 Jan 2011, 02:44 PM
Ah, point taken.  I see how IsReadOnlyBinding works.

Great support and thank you.
John.
Tags
GridView
Asked by
John
Top achievements
Rank 1
Answers by
Maya
Telerik team
John
Top achievements
Rank 1
Share this question
or