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

Underlying Object

2 Answers 83 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Kim
Top achievements
Rank 1
Kim asked on 21 Mar 2012, 05:30 AM

 

 

 

 

 

Hi, I have a radgrid bound to an iList of objects. My list has objects of type "Voucher"

The user can select multiple rows in the grid.

I can enum through the selected rows using;

 

 

 

 


for each x in grd.SelectedItems

next

How can i get a reference to the underlying "Voucher" object the row is bound to?

thx!

 

 

 

 

 

 

 

 

2 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 21 Mar 2012, 08:12 AM
Hello Kim,

I have created a sample scenario with your requirement. I hope it will help.
aspx:
<form id="form1" runat="server">
  <div>
    <asp:ScriptManager ID="ScriptManager1" runat="server">
    </asp:ScriptManager>
     <telerik:RadGrid ID="RadGrid1" AllowMultiRowSelection="true" runat="server" AutoGenerateColumns="true" OnNeedDataSource="RadGrid1_NeedDataSource">
       <MasterTableView DataKeyNames="Id">
       </MasterTableView>
       <ClientSettings Selecting-AllowRowSelect="true">
       </ClientSettings>
     </telerik:RadGrid>
  </div>
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
VB:
Public Shared dataSource As List(Of Voucher)
Protected Sub RadGrid1_NeedDataSource(sender As Object, e As Telerik.Web.UI.GridNeedDataSourceEventArgs)
    dataSource = New List(Of Voucher)()
    dataSource.Add(New Voucher() With { _
        .Id = 1, _
        .Name = "Nancy" _
    })
    dataSource.Add(New Voucher() With { _
        .Id = 2, _
        .Name = "Andrew" _
    })
    Me.RadGrid1.DataSource = dataSource
End Sub
Public Class Voucher
    Public Property Id() As Integer
        Get
            Return m_Id
        End Get
        Set
            m_Id = Value
        End Set
    End Property
    Private m_Id As Integer
    Public Property Name() As String
        Get
            Return m_Name
        End Get
        Set
            m_Name = Value
        End Set
    End Property
    Private m_Name As String
End Class
Protected Sub Button1_Click(sender As Object, e As EventArgs)
    For Each x As GridDataItem In RadGrid1.SelectedItems
        Dim id As Integer = Convert.ToInt16(x.GetDataKeyValue("Id").ToString())
        Dim vcItem As Voucher = dataSource.Find(Function(p As Voucher) p.Id.Equals(id))
    Next
End Sub

Thanks,
Shinu.
0
Kim
Top achievements
Rank 1
answered on 26 Mar 2012, 12:57 AM
Yes it helps, thanks.


Tags
Grid
Asked by
Kim
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Kim
Top achievements
Rank 1
Share this question
or