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

In RadGridView disable cells in each row for particular column

13 Answers 358 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Sheetal
Top achievements
Rank 1
Sheetal asked on 21 Feb 2011, 11:16 AM

Hi,

I am using RadGridView and using MVVM model. In Grid I want to disable cells in each row for particular column. I have written below code but the (GridViewRow row) is always null but I get the number of rows in grid.
Kindly help, so that I can get GridViewRow.

public void OnDataLoadedCommandExecuted(DataLoadedCommandParameter commandParameter)
        {
            RadGridView currentGrid = commandParameter.CurrentGridView;

           
              
                foreach (object item in currentGrid.Items)
                {
                    GridViewRow row = currentGrid.ItemContainerGenerator.ContainerFromItem(item) as GridViewRow;

                    if (row != null)
                    {
                        GridViewCellBase cell = (from c in row.Cells
                                                 where c.Column.UniqueName == "Test"
                                                 select c).FirstOrDefault();
                        if (cell != null)
                        {
                            cell.IsEnabled = false;
                        }
                    }
                }
           
        }

13 Answers, 1 is accepted

Sort by
0
Maya
Telerik team
answered on 21 Feb 2011, 02:03 PM
Hello Sheetal,

You may try to handle the RowLoaded event instead. However, I may suggest you to work withe IsReadOnlyBinding property. Thus you will be able to decide on row level which cell should be editable or not. For further reference, please take a look at our online documentation.
 

Best wishes,
Maya
the Telerik team
Registration for Q1 2011 What’s New Webinar Week is now open. Mark your calendar for the week starting March 21st and book your seat for a walk through all the exciting stuff we ship with the new release!
0
Jerry T.
Top achievements
Rank 1
answered on 24 Jun 2011, 07:14 PM
That link to the documentation really help at all. It only shows some settings in the XAML.

How can the RowLoaded event be used to set particular cells as disabled?

I have a grid where certain cells need to be disabled, not an entire row nor an entire column.

I'm coming from the world of AJAX controls and finding it difficult to find good examples on customizing the grid content for these Silverlight controls.

Thanks.

Jerry
0
Maya
Telerik team
answered on 27 Jun 2011, 07:51 AM
Hi Jerry T.,

I am sending you a sample project illustrating the implementation of IsReadOnlyBinding so that you can make some of the cells in a row read-only or not. If you want to keep up the grey color of a disabled cell, you may use a CellStyleSelector
Considering the usage of RowLoaded event, you may do something similar to:

void clubsGrid_RowLoaded(object sender, Telerik.Windows.Controls.GridView.RowLoadedEventArgs e)
        {
              if(e.Row is GridViewRow)
              {
                  var cells = (e.Row as GridViewRow).ChildrenOfType<GridViewCell>();
                  ((GridViewCell)cells.FirstOrDefault()).IsEnabled = false;
              }
        }

Still, I would recommend you using the first approach.
Let me know if you need any further assistance.
  Best wishes,
Maya
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
Jerry T.
Top achievements
Rank 1
answered on 27 Jun 2011, 03:36 PM
Thanks.

I will try the 1st approach.

I was using the 2nd approach (the code you posted directly) and noticed that cells that were in columns that were not shown on-screen (I have a lot of columns in this grid and have to scroll to see the rest) are not contained in the cells object.  Is this a bug or am I missing something?


EDIT: Ahh...the  EnableColumnVirtualization setting
0
Maya
Telerik team
answered on 28 Jun 2011, 07:40 AM
Hello Jerry T.,

Indeed, when the virtualization of the grid is turned on (which is by default), only the visible visual elements will be created. That is why it is not recommended to work with those visual elements and their values as they will be recycled and reused. Switching off the virtualization, on the other hand, may lead to some performance degradation if you handle a lot of data.
Thus I would recommend you to follow the approach with using IsReadOnlyBinding. Let me know if you need any further assistance.

All the best,
Maya
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
Jerry T.
Top achievements
Rank 1
answered on 28 Jun 2011, 01:06 PM
Maya,

You wrote: "Switching off the virtualization, on the other hand, may lead to some performance degradation if you handle a lot of data."

In that case, is there something similar to the DataKeyNames from the AJAX RadGrid that exists in the Silverlight control?  Having access to columns returned from the datasource inside the grid, but not visible to the user, comes in handy quite a bit.  I've already found that adding additional columns to the RadGridView but setting them as IsVisible="False" means they don't show up in the cells() collection either.  What's the purpose of that? We need more control over what we store in the RadGridView.

Thanks.

Jerry
0
Maya
Telerik team
answered on 28 Jun 2011, 04:10 PM
Hi Jerry T.,

Generally, the recommended approach is to work with the underlying data item, not with the visual elements. Thus, instead of finding all the columns and the corresponding cells, you may get the properties for each items and get their values.

Kind regards,
Maya
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
Jerry T.
Top achievements
Rank 1
answered on 28 Jun 2011, 04:12 PM
Thanks, Maya.

Would you have any sample code for getting to that data? I'm assuming this goes something like MyGrid.Items(rowIndex).whatever  or something like that?
0
Maya
Telerik team
answered on 28 Jun 2011, 04:18 PM
Hello Jerry T.,

You may get a particular item as follows:

Club club = this.clubsGrid.Items[1] as Club;
string name = club.Name;

In the case above Club is the business object with a Name property. 

All the best,
Maya
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
Jerry T.
Top achievements
Rank 1
answered on 28 Jun 2011, 04:19 PM
Does that index allow accessing the Items data via the UniqueName attribute for the column?

Not a fan of just using hard-coded numeric values (unless I created enumerated values for them)
0
Maya
Telerik team
answered on 29 Jun 2011, 09:37 AM
Hello Jerry T.,

The UniqueName property of the column will correspond (unless being set differently) to the name of the corresponding property from the business object.
 

All the best,
Maya
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
Subham
Top achievements
Rank 1
answered on 16 May 2017, 06:24 PM

Can you please provide sample code demonstrating use of isreadonly binding in c#

0
Dilyan Traykov
Telerik team
answered on 19 May 2017, 11:04 AM
Hello Subham,

The project my colleague provided gives a good demonstration of how to use the IsReadOnlyBinding.

The respective article also refers to a good illustration of its use in our SDK Samples Browser.

If what you're asking is how to achieve the binding in code-behind, here's an example:

// apply the binding per row
this.RadGridView.IsReadOnlyBinding = new Binding("IsNameReadOnly");
 
// apply the binding per column
(this.RadGridView.Columns[0] as GridViewDataColumn).IsReadOnlyBinding = new Binding("IsNameReadOnly");

I hope you find these resources helpful.

Regards,
Dilyan Traykov
Telerik by Progress
Try our brand new, jQuery-free Angular 2 components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
Tags
GridView
Asked by
Sheetal
Top achievements
Rank 1
Answers by
Maya
Telerik team
Jerry T.
Top achievements
Rank 1
Subham
Top achievements
Rank 1
Dilyan Traykov
Telerik team
Share this question
or