Is there a way to get the GridViewRow for a record in a RadGridView?
The reason I ask is that I'm trying to implement some behavior where pressing Tab in the last cell of the Grid creates a new row and then puts the first cell for the newly added record in edit mode. I have read this post and this post and have everything working except getting a cell in the new row in edit mode.
Thanks,
Joel
16 Answers, 1 is accepted
I'm attaching a sample project which demonstrates how to set cell in the NewRow in an edit mode.
Hope this helps.
Sincerely yours,
Nedyalko Nikolov
the Telerik team
Check out Telerik Trainer, the state of the art learning tool for Telerik products.

Thanks for your reply. Sorry I wasn't more clear in my question as to what I'm trying to achieve.
I'm not actually using the "New Row" (i.e. GridViewNewRow) to add new records to the grid. I'm just programmatically adding another item to the collection that the grid is bound to when the Tab key is pressed and the user was in the last cell of the grid. This new item then shows up as the last row in the grid.
I can get the Record for the newly added item by using the FindRecord() method of the RadGridView. I was wondering if it was possible to get the GridViewRow that is used to display that Record. Once I have the GridViewRow then I can use the code that you sent in your sample project to put the first column of that row into edit mode.
Thanks,
Joel
Here is an example:
void Window1_Loaded(object sender, RoutedEventArgs e) |
{ |
Record record = RadGridView1.ItemsControl.Records[3]; |
int recordIndex = RadGridView1.ItemsControl.Records.IndexOf(record); |
ItemsGenerator gen = ((BaseItemsControl)(RadGridView1.ItemsControl)).ItemsGenerator; |
GridViewRow row = (GridViewRow)gen.GenerateItemAtIndex(recordIndex); |
row.Items.Where(i => i.Content.ToString() == "AROUT").Select(i=>i.Background = Brushes.Red).Any(); |
} |
Greetings,
Vlad
the Telerik team
Check out Telerik Trainer, the state of the art learning tool for Telerik products.

Thanks for your reply, however, this code doesn't seem to work for me. When I run it the row.Items property of the row that was generated doesn't contain any items so an exception is thrown on the call to rows.Items.Where().
Is there really no easy way to access the rows in the grid? I would've expected to be able to do something like RadGridView1.Rows[3].Cells[0].IsInEditMode = true.
Joel
Please check this post for more info:
http://blogs.telerik.com/vladimirenchev/posts/09-02-03/Easy_programmatic_UI_customization_for_WPF_and_Silverlight.aspx
Greetings,
Vlad
the Telerik team
Check out Telerik Trainer, the state of the art learning tool for Telerik products.

That looks perfect. I am now really looking forward to the next release.
Thanks,
Joel

cell.Content = "Changed!";
Well, you just need to extract the value using the "values" object:
var values = playersGrid.ChildrenOfType<GridViewCell>().Where(c => c.Column.UniqueName ==
"Priority"
).FirstOrDefault();
object
myCellValue = values.Value;
If that is not working most probably there is no column with UniqueName "Priority".
May I suggest another approach that might work better for your scenario. If you create a simple IValueConverter you will be able to easily convert between a boolean value and its respective image.
public
class
BooleanToImageConverter : IValueConverter
{
public
object
Convert(
object
value, Type targetType,
object
parameter,CultureInfo culture)
{
bool
booleanValue = (
bool
)value;
if
(booleanValue)
return
new
BitmapImage(
new
Uri(
"trueImage.png"
, UriKind.RelativeOrAbsolute));
else
return
new
BitmapImage(
new
Uri(
"falseImage.png"
, UriKind.RelativeOrAbsolute));
}
public
object
ConvertBack(
object
value, Type targetType,
object
parameter, CultureInfo culture)
{
throw
new
NotImplementedException();
}
}
This converter will convert any boolean value to its image representation. We can easily plug this converter into the grid and get automatic boolean to image conversion.
<
Grid
>
<
Grid.Resources
>
<
local:BooleanToImageConverter
x:Key
=
"ToImageConverter"
/>
</
Grid.Resources
>
<
telerik:RadGridView
Name
=
"playersGrid"
Grid.Row
=
"1"
AutoGenerateColumns
=
"False"
ColumnsWidthMode
=
"Auto"
>
<
telerik:RadGridView.Columns
>
<
telerik:GridViewColumn
Header
=
"MyFirst Boolean Column"
>
<
telerik:GridViewColumn.CellTemplate
>
<
DataTemplate
>
<
Image
Source="{Binding IsSelected,
Converter={StaticResource ToImageConverter}}"/>
</
DataTemplate
>
</
telerik:GridViewColumn.CellTemplate
>
</
telerik:GridViewColumn
>
</
telerik:RadGridView.Columns
>
</
telerik:RadGridView
>
</
Grid
>
I have also attached a sample project that demonstrates this approach.
Greetings,
Milan
the Telerik team
Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.

Thank you very much for valuable response to me. I have bit different scenerio where i m generating the columns dynamically by code
so there is no coding in xaml of the radgridview. In that case how to use above converter without specifying it information in xaml of radgridview. Also please mention how to retrive all value in lamda expresion. I m using
var values = radgridview.Children<GridViewCell>().Where(c=>c.column.uniquename =="Priority").All()
i dont know that should be the precidate in All. so that i iterate though the all values and check for True and False according i put the respective images.
I know this are some trival questions. But as i m newbie help me out.
Well, if you are able to update to one of your latest internal releases you will be able to use one of our new column types called GridViewImageColumn. It will allows you to easily add dynamic image columns (you still have to use the BooleanToImageConverter).
I have prepared a sample application that demonstrates how you can add an image column (CreateImageColumnForProperty method in sample application) to display and image (base on a boolean value). Both XAML and Code-behind implementations are demonstrated. I have also demonstrated how you can get all cells are either "false" or "true".
In my opinion the best approach is to use our new column type since it is very easy to use and it is not necessary to write many lines of custom code.
Hope this information is helpful.
All the best,
Milan
the Telerik team
Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.

Thank you very much for the your valuable response,i learn a lot from the same.Well interesting thing i found is RadGridview is working so nicely without even Conveter below is my code.
var isUnReadCellsValue = e.Row.ChildrenOfType<GridViewCell>().Where(c => c.Column.UniqueName == EmailMessageController.COLUMNISUNREAD).ToList();
if (isUnReadCellsValue != null)
{
string cellvalue = "True";
foreach (var value in isUnReadCellsValue)
{
if (cellvalue == value.Value.ToString())
{
var readedImage = new Image() { Height = 10, Width = 10, HorizontalAlignment = HorizontalAlignment.Center };
readedImage.Source =
new BitmapImage(new Uri(ImageDB.Email.READED, UriKind.RelativeOrAbsolute));
value.Value = readedImage;
}
else
{
var isUnreadImage = new Image() { Height = 10, Width = 10, HorizontalAlignment = HorizontalAlignment.Center };
isUnreadImage.Source =
new BitmapImage(new Uri(ImageDB.Email.UNREADED, UriKind.RelativeOrAbsolute));
value.Value = isUnreadImage;
}
}
}
is there any pitfall in above code ??? Sir
Thanking you,
Miral Shah
You could do it this way but you should probably use the cell.Content property instead of cell.Value. Still, I believe that the GridViewImageColumn approach is better since you will get some functionality out of the box. For example, if one of your boolean property changes (provided that your data items implement INotifyPropertyChanged) the image in the respective cell will also update.
Sincerely yours,
Milan
the Telerik team
Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.

I want to get a specific row and i am using the following code:
GridViewRow
row = radGFormSections.ChildrenOfType<GridViewRow>().Where(r => r.ChildrenOfType<GridViewCell>().Where(c => c.Column.UniqueName == "Id").Where(v => v.Value.ToString() == formSection.Id.ToString()).Any()).First();
My problem is, Id colum is invisible in my WPF RadGridView. Though i have a record, i am getting an error that there are no elements in the sequence.
How to get a specific row depending on a value of a invisible row.
Thanks in advance,
Jaya.
You could instead try to search your data items instead and then find the row that corresponds to the specified data item:
private
void
Button_Click(
object
sender, RoutedEventArgs e)
{
var foundItem =
this
.grid.Items.OfType<MyDataType>().Where(item => item.Id ==
"SomeId"
).FirstOrDefault();
var row =
this
.grid.ItemsGenerator.ContainerFromItem(foundItem);
// or, if you are using version prior Q3 2009
var row =
this
.grid.ItemsControl.ItemsGenerator.ContainerFromItem(foundItem);
}
Greetings,
Milan
the Telerik team
Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.

GridViewRow row = (GridViewRow)gen.GenerateItemAtIndex(recordIndex); |
Thank you in advance!!
Hi Victor,
In that case you won't be able to get a row if row virtualization is turned on (on by default). The options there are to turn off row vitualization by setting EnableRowVirtualization to false or rework the application logic so that it works with the data instead of the UI elements like rows.
Kind regards,
Milan
the Telerik team