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

Get GridViewRow for Record

16 Answers 1153 Views
GridView
This is a migrated thread and some comments may be shown as answers.
JP
Top achievements
Rank 1
JP asked on 30 Jan 2009, 11:32 PM
Hello,

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

Sort by
0
Nedyalko Nikolov
Telerik team
answered on 02 Feb 2009, 03:34 PM
Hi Joel,

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.
0
JP
Top achievements
Rank 1
answered on 02 Feb 2009, 03:47 PM
Hello Nedyalko,

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


0
Vlad
Telerik team
answered on 03 Feb 2009, 09:38 AM
Hi 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.
0
JP
Top achievements
Rank 1
answered on 03 Feb 2009, 04:22 PM
Hi Vlad,

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
0
Vlad
Telerik team
answered on 03 Feb 2009, 04:27 PM
Hello 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.
0
JP
Top achievements
Rank 1
answered on 03 Feb 2009, 04:34 PM
Hi Vlad,

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

Thanks,

Joel
0
miral shah
Top achievements
Rank 2
answered on 12 Oct 2009, 11:01 AM
Respect Sir,

Below is the code for retrieving particular cell value , which got from your above mentioned blog.   

var cell = RadGridView1.ChildrenOfType<GridViewCell>().Where(c => c.Content.ToString() =="ALFKI").First(); 
cell.Content = "Changed!";

But it didnt seem to work for me. 

Below is my case. 

I m having radgridview loaded with following colums

Priority   WithAttachment  IsUnread 
 True           True                True
 False          True               True
 True            False              True

Now i have to populate the different Images in place of True and False respectively in all columns. As far as my knowledge goes i wrote  like this

 void gvEmail_RowLoaded(object sender, RowLoadedEventArgs e)
        {
           
                        var values = gvEmail.ChildrenOfType<GridViewCell>().Where(c => c.Column.UniqueName =="Priority").First();


            
        }
          

But i m not getting any content or property of the Priority Column. so that i m unable to proceed further for checking the value is True then populate image1 else image2.

I hope you understand my problem. Please throw some light on it Asap.

Thanking you 
Miral Shah
0
Milan
Telerik team
answered on 12 Oct 2009, 04:55 PM
Hi miral shah,

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.
0
miral shah
Top achievements
Rank 2
answered on 13 Oct 2009, 08:41 AM
Respected Sir,

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.
0
Milan
Telerik team
answered on 13 Oct 2009, 02:09 PM
Hello miral shah,

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.
0
miral shah
Top achievements
Rank 2
answered on 14 Oct 2009, 08:25 AM
Respected Mr. Milan,


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
0
Milan
Telerik team
answered on 14 Oct 2009, 03:43 PM
Hi 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.
0
Victor
Top achievements
Rank 1
answered on 06 Nov 2009, 09:57 PM
Hi,

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.
0
Milan
Telerik team
answered on 07 Nov 2009, 10:12 AM
Hi Victor,

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.
0
Ana
Top achievements
Rank 1
answered on 17 Feb 2011, 09:56 PM
            GridViewRow row = (GridViewRow)gen.GenerateItemAtIndex(recordIndex); 
This would only work if the row is visible. What if I want a row that is hidden? What can I do in that case?

Thank you in advance!!
0
Milan
Telerik team
answered on 18 Feb 2011, 08:18 AM

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
Tags
GridView
Asked by
JP
Top achievements
Rank 1
Answers by
Nedyalko Nikolov
Telerik team
JP
Top achievements
Rank 1
Vlad
Telerik team
miral shah
Top achievements
Rank 2
Milan
Telerik team
Victor
Top achievements
Rank 1
Ana
Top achievements
Rank 1
Share this question
or