In RadGridView disable cells in each row for particular column

12 posts, 0 answers
  1. Sheetal
    Sheetal avatar
    1 posts
    Member since:
    Jan 2011

    Posted 21 Feb 2011 Link to this post

    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;
                            }
                        }
                    }
               
            }

  2. Maya
    Admin
    Maya avatar
    4062 posts

    Posted 21 Feb 2011 Link to this post

    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!
  3. DevCraft banner
  4. Jerry T.
    Jerry T. avatar
    118 posts
    Member since:
    Jul 2012

    Posted 24 Jun 2011 Link to this post

    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
  5. Maya
    Admin
    Maya avatar
    4062 posts

    Posted 27 Jun 2011 Link to this post

    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
  6. Jerry T.
    Jerry T. avatar
    118 posts
    Member since:
    Jul 2012

    Posted 27 Jun 2011 Link to this post

    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
  7. Maya
    Admin
    Maya avatar
    4062 posts

    Posted 28 Jun 2011 Link to this post

    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
  8. Jerry T.
    Jerry T. avatar
    118 posts
    Member since:
    Jul 2012

    Posted 28 Jun 2011 Link to this post

    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
  9. Maya
    Admin
    Maya avatar
    4062 posts

    Posted 28 Jun 2011 Link to this post

    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
  10. Jerry T.
    Jerry T. avatar
    118 posts
    Member since:
    Jul 2012

    Posted 28 Jun 2011 Link to this post

    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?
  11. Maya
    Admin
    Maya avatar
    4062 posts

    Posted 28 Jun 2011 Link to this post

    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
  12. Jerry T.
    Jerry T. avatar
    118 posts
    Member since:
    Jul 2012

    Posted 28 Jun 2011 Link to this post

    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)
  13. Maya
    Admin
    Maya avatar
    4062 posts

    Posted 29 Jun 2011 Link to this post

    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
Back to Top
DevCraft banner