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

Programmatically retrieve selected row values from radgridview

10 Answers 788 Views
GridView
This is a migrated thread and some comments may be shown as answers.
CQT
Top achievements
Rank 1
CQT asked on 13 Feb 2013, 09:39 AM
Hello,
I'm dealing with a radgridview and I'm in trouble while trying to retrieve row values from a radgridview.

I'm using telerik wpf control q2 2012.

My gridview is named Radgridview1 and has 5 colums (named "ID", "name", "surname", "address", "city")
I know I can retrive, for example, the value of the colum "ID" of the selected row using this code:

Dim myvalue As String = (RadGridView1.SelectedItem).ID

I have an array containing names of my five columns. I want to cycle my array and retrive values of each column of the selected row.

To do this I should be able to replace ".ID" in my code above with a variable retrievd from my list/array. How I can do this?

How I can replase the phisical name of the colum with the value of my string variable?

Anyone has an example?

King Regards

Simone

10 Answers, 1 is accepted

Sort by
0
Vlad
Telerik team
answered on 13 Feb 2013, 09:43 AM
Hello,

 You can loop SelectedItems, cast each item to your type and access desired properties. 

All the best,
Vlad
the Telerik team

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

0
CQT
Top achievements
Rank 1
answered on 13 Feb 2013, 10:17 AM
Thanks for your reply Vlad,

I tried using this code for casting, but no success, the idea was to replace string "ID" with the actual content of my list. Which sholud be the object type for casting? DataRow isn't accepted :

For Each Item In Radgridview1.SelectedItems

    CType(Item , DataRow)("ID")

Next

0
Vlad
Telerik team
answered on 13 Feb 2013, 11:51 AM
Hi,

 Are you using DataRows? How your grid is bound?

Regards,
Vlad
the Telerik team

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

0
CQT
Top achievements
Rank 1
answered on 13 Feb 2013, 12:33 PM
Hi, My grid is bound to a Table using linq to Entity Framework, no autogenerated colums.
0
Vlad
Telerik team
answered on 13 Feb 2013, 12:37 PM
Hi,

 Can you post the code?

Kind regards,
Vlad
the Telerik team

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

0
CQT
Top achievements
Rank 1
answered on 13 Feb 2013, 01:09 PM
Hi Vlad, here is my xaml code, there is the radgridview with colums, note that one colum contains a Gridviewcombobox because I need to select a role from a list. This combo takes an id of the role from a from a contacts table and shows a description of the role taken from another table (role table):

<Window x:Class="Window2"
              xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <telerik:RadGridView x:Name="RadGridView1" AutoGenerateColumns="False" >
            <telerik:RadGridView.Columns>
                <telerik:GridViewDataColumn DataMemberBinding="{Binding Nome}" UniqueName="Nome"/>
                <telerik:GridViewComboBoxColumn
                                  DataMemberBinding="{Binding IDRuolo}"
                                  ItemsSourceBinding="{Binding RuoliDisponibili}"
                                  SelectedValueMemberPath="RuoloID"
                                  DisplayMemberPath="Descrizione" />
                <telerik:GridViewDataColumn DataMemberBinding="{Binding IDContatto}" />
            </telerik:RadGridView.Columns>
        </telerik:RadGridView>
    </Grid>
</Window>



This is code behind, in sub new I retrieve data from tables and bound to radgridview, while in RadGridView1_RowEditEnded I try to get changed data from radgridview and save to database.


Imports System.Collections.Generic
Imports System.Linq
Imports System.Text
Imports System.Windows
Imports System.Windows.Controls
Imports System.Windows.Data
Imports System.Windows.Documents
Imports System.Windows.Input
Imports System.Windows.Media
Imports System.Windows.Media.Imaging
Imports System.Windows.Navigation
Imports System.Windows.Shapes
Imports System.ComponentModel
Imports Library.Utility
Imports System.Reflection
Imports System.Data
Imports Telerik.Windows.Controls
 
''' <summary>
''' Interaction logic for MainWindow.xaml  
''' </summary>
Public Class Window2
    Public Sub New()
        InitializeComponent()
         
        '-------------------- I retrieve a list of contacts from table tab_ContattoInternoCF and a list of roles from tab_Ruolo, then i bound results to radgridview1 ------------------
 
        Dim elenco As New List(Of Referenti)()
        Dim lista = (From q In db_conn.tab_ContattoInternoCF Select New Referenti With {.Nome = q.Nome, .IDRuolo = q.IDRuolo, .IDContatto = q.IdContatto})
 
 
        Dim ElencoReferenti As New List(Of Referenti)()
 
 
        For Each l In lista
            ElencoReferenti.Add(l)
        Next
 
 
        Dim qry = (From r In db_conn.tab_Ruolo Select New Ruoli With {.RuoloID = r.RuoloID, .Descrizione = r.Descrizione})
 
        For Each r In qry
            For Each ref In ElencoReferenti
                ref.RuoliDisponibili.Add(r)
            Next
 
        Next
 
    '----------------- grid bounding -----------------------
 
        Me.RadGridView1.ItemsSource = ElencoReferenti
 
        AddHandler Europe.PropertyChanged, New PropertyChangedEventHandler(AddressOf Europe_PropertyChanged)
        AddHandler Asia.PropertyChanged, New PropertyChangedEventHandler(AddressOf Asia_PropertyChanged)
    End Sub
 
'-----------------when I end row editing I want to save changes to my tables in database, so I have to retrieve data from gridview and save them to database -----------------
 
    Private Sub RadGridView1_RowEditEnded(sender As Object, e As Telerik.Windows.Controls.GridViewRowEditEndedEventArgs) Handles RadGridView1.RowEditEnded
        Dim ContattoInternoCF As New Library.tab_ContattoInternoCF
        Dim idcont As Integer = RadGridView1.SelectedItem.Idcontatto
        Dim query = (From q In db_conn.tab_ContattoInternoCF
                              Where q.IdContatto = idcont
                              ).FirstOrDefault
 
 
        Dim props1 = query.GetType.GetProperties()
        For Each prop In props1
            For i As Integer = 0 To RadGridView1.Columns.Count - 1
                If RadGridView1.Columns.Item(i).UniqueName = prop.Name Then
 
 
                    For Each Item In RadGridView1.SelectedItems
 
                        Dim myData As Referenti = TryCast(Item, Referenti)
 
                        MessageBox.Show(prop.Name & " " & myData.ToString)
 
                    Next
 
                    prop.SetValue(query, e.OldValues(prop.Name), Nothing)
                End If
 
            Next
        Next
 
        db_conn.SaveChanges()
    End Sub
End Class
 
Public Class Referenti
    Implements INotifyPropertyChanged
    Public Sub New()
        Me.RuoliDisponibili = New List(Of Ruoli)()
    End Sub
 
    Public Property Nome() As String
        Get
            Return m_Nome
        End Get
        Set(value As String)
            m_Nome = value
        End Set
    End Property
    Private m_Nome As String
 
    Private m_IDRuolo As Integer
    Public Property IDRuolo() As Integer
        Get
            Return Me.m_IDRuolo
        End Get
        Set(value As Integer)
            Me.m_IDRuolo = value
 
            RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs("IDRuolo"))
        End Set
    End Property
 
    Private m_IDContatto As Integer
    Public Property IDContatto() As Integer
        Get
            Return Me.m_IDContatto
        End Get
        Set(value As Integer)
            Me.m_IDContatto = value
 
            RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs("IDContatto"))
        End Set
    End Property
 
    Public Property RuoliDisponibili() As List(Of Ruoli)
        Get
            Return m_RuoliDisponibili
        End Get
        Set(value As List(Of Ruoli))
            m_RuoliDisponibili = value
        End Set
    End Property
    Private m_RuoliDisponibili As List(Of Ruoli)
 
 
    Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged
 
End Class
 
Public Class Ruoli
    Public Property RuoloID() As Integer
        Get
            Return m_RuoloID
        End Get
        Set(value As Integer)
            m_RuoloID = value
        End Set
    End Property
    Private m_RuoloID As Integer
    Public Property Descrizione() As String
        Get
            Return m_Descrizione
        End Get
        Set(value As String)
            m_Descrizione = value
        End Set
    End Property
    Private m_Descrizione As String
End Class
0
Vlad
Telerik team
answered on 13 Feb 2013, 01:17 PM
Hello,

 As far as I can see you do not have any DataTables nor DataRows. The grid in your case is bound to List(Of Referenti):

Dim ElencoReferenti As New List(Of Referenti)()
....
Me.RadGridView1.ItemsSource = ElencoReferenti
...

All the best,
Vlad
the Telerik team

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

0
CQT
Top achievements
Rank 1
answered on 13 Feb 2013, 01:25 PM
I'M sorry but I don't understand, one more question:
you mean I cannot achieve my goal? I can't retrieve modified values from radgridview  after editing?

Best Regards
0
Vlad
Telerik team
answered on 13 Feb 2013, 01:29 PM
Hello,

 As I said in my first reply you can use SelectedItems collection to retrieve property values for selected items. To access these properties you should cast these items to your item type - in your case Referenti.

Regards,
Vlad
the Telerik team

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

0
CQT
Top achievements
Rank 1
answered on 13 Feb 2013, 02:09 PM
Hi, after your first reply I made this:

For Each Item In RadGridView1.SelectedItems
Dim myData As Referenti = TryCast(Item, Referenti)
MessageBox.Show(prop.Name & " " & myData.ToString)
Next

to get values I have to put, for example, myData.IdRuolo, where IdRuolo is a field of my list. In this case I have to phisically write myData.IdRuolo, but I don't want this, in my code the String "IdRuolo" is contained in a variable (prop.Name), so I need to know how to use it instead of the string "IdRuolo".

All my work was made to build a radgridview bounded to a table, having a column containing a combobox which takes his ID from this table, and a description from another one. This radgridview should be editable and after editing data should be saved back in the table.
In case there is no solution to the problem above, can you tell me a way to accomplish this goal?
Here is a screenshot of my grid:




Best Regards
Tags
GridView
Asked by
CQT
Top achievements
Rank 1
Answers by
Vlad
Telerik team
CQT
Top achievements
Rank 1
Share this question
or