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

[Solved] How to get SelectedItem(s)

2 Answers 155 Views
GridView
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Tobitux
Top achievements
Rank 1
Tobitux asked on 30 Aug 2009, 10:22 PM
Hi Everybody,

because I need to create columns at runtime I use the GridView with the very cool DataTable class. What I'm trying to accomplish is to retrieve the SelectedItems from GridView. My problem is that the SelectedItems property shows the correct number of SelectedItems but all items are Nothing. I can't find out which rows are selected and doesn't get the record back. Here is the code I use to create the GridView. It's just a reduced test code without any deep sense.
           
Dim tester As New ObservableCollection(Of Test) 
tester.Add(New Test With {.Name = "Peter"}) 
tester.Add(New Test With {.Name = "Paul"}) 
tester.Add(New Test With {.Name = "And"}) 
tester.Add(New Test With {.Name = "Mary"}) 
 
Dim rnd As Random = New Random 
Dim table As New DataTable 
 
table.Columns.Add(New DataColumn() With {.ColumnName = "Name", .DataType = GetType(String)}) 
table.Columns.Add(New DataColumn() With {.ColumnName = "DC", .DataType = GetType(Object)}) 
 
            For Each test In tester 
                Dim row As DataRow = New DataRow 
                row("Name") = test.Name 
                row("DC") = test 
                table.Rows.Add(row) 
            Next 
 
            RadGridView.AutoGenerateColumns = False 
            RadGridView.Columns.Clear() 
 
            ' Name 
            Dim c = New GridViewDataColumn 
            c.Header = "Name" 
            Dim b = New Binding("Name"
            c.DataMemberBinding = b 
            RadGridView.Columns.Add(c) 
 
            ' Template 
            Dim c1 = New GridViewDataColumn() 
            c1.Header = "Template" 
            c1.CellTemplate = Resources("MyTemplate"
            b = New Binding("DC"
            c1.DataMemberBinding = b 
            RadGridView.Columns.Add(c1) 
 
 
            RadGridView.ItemsSource = table
<UserControl xmlns:telerikGridView="clr-namespace:Telerik.Windows.Controls;assembly=Telerik.Windows.Controls.GridView"  xmlns:data="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data"  x:Class="DatagridEval.MainPage" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"  
    mc:Ignorable="d" d:DesignWidth="640" d:DesignHeight="480"
 
    <UserControl.Resources> 
    <DataTemplate x:Key="MyTemplate"
        <StackPanel DataContext="{Binding DC}"
            <Button Content="{Binding Name}"></Button> 
        </StackPanel> 
    </DataTemplate> 
    </UserControl.Resources> 
     
    <Grid x:Name="LayoutRoot"
      <telerikGridView:RadGridView x:Name="RadGridView" AutoGenerateColumns="False"></telerikGridView:RadGridView> 
  </Grid> 
</UserControl> 
 

Thanks in advance,
Tobias

2 Answers, 1 is accepted

Sort by
0
Tobitux
Top achievements
Rank 1
answered on 31 Aug 2009, 06:58 AM
Hi,

I even checked out the SelectionChanged Event and discover the SelectionChangeEventArgs. But there I have the same problem. The AddedItems as well as the AddedRecords collection have the correct number of selected rows. But the objects in the AddedItem collection and in AddedRecords(0).Data are Nothing :-(

What must I do to retrieve the SelectedItem/Selected record value?

Thank you,
Tobias
0
Tobitux
Top achievements
Rank 1
answered on 31 Aug 2009, 10:36 AM
Hi Everybody,

I found the solution and would like to share it with you.

As Vlad described here in the Comment from 28 April 2009 you have to do the following to access the SelectedItem and his properties:

if (this.RadGridView1.SelectedRecords.Count == 1)  
{  
    object objBusinessData = RadGridView1.SelectedItem;  
     PropertyInfo[] props = objBusinessData.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public); 
 
    string sValue = (string)props.Where(pi=>pi.Name == "file_data").FirstOrDefault().GetValue(objBusinessData, null); 
    string sAssetTag = (string)props.Where(pi=>pi.Name == "asset_tag").FirstOrDefault().GetValue(objBusinessData, null); 
 
    byte[] fileBytes = Convert.FromBase64String(sValue); 
 
    MemoryStream ms = new MemoryStream(fileBytes); 
    BitmapImage image = new BitmapImage(); 
    ms.Position = 0; 
    image.SetSource(ms); 
    myImage.Source = image; 
 

Yeah, It works!!!
Tobias

Tags
GridView
Asked by
Tobitux
Top achievements
Rank 1
Answers by
Tobitux
Top achievements
Rank 1
Share this question
or