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
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.
Maya
the Telerik team
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
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
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
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.
Maya
the Telerik team
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
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.
Maya
the Telerik team
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?
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
Not a fan of just using hard-coded numeric values (unless I created enumerated values for them)
The UniqueName property of the column will correspond (unless being set differently) to the name of the corresponding property from the business object.
Maya
the Telerik team
Can you please provide sample code demonstrating use of isreadonly binding in c#
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