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

Binding Radgrid to Business Object Displays Empty Values

1 Answer 56 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Douglas
Top achievements
Rank 1
Douglas asked on 11 Jan 2012, 03:30 PM
I am trying to bind a RadGrid to a generic list of business objects using the OnNeedDataSource event. The grid displays the correct number of rows, however, the values for the fields are empty. I can bind to a datasource object without a problem, but I am going to be adding additional functionality where this method will be necessary. For simplicity sake, I am just trying to bind a single exposed property from the custom class object in the list.

.ASPX
<formid="form1"runat="server"
    <telerik:RadScriptManagerID="RadScriptManager1"  runat="server"
            
    </telerik:RadScriptManager
    <div
     <telerik:RadGridrunat="server"AllowPaging="True"ID="RadGrid2"OnNeedDataSource="RadGrid2_NeedDataSource"Width="600px"PageSize="8"
            <MasterTableViewWidth="100%"
                <Columns
                     <telerik:GridBoundColumnHeaderText="Project Name"DataField="name"></telerik:GridBoundColumn
                </Columns
                <NoRecordsTemplate
                    <divstyle="height: 30px; cursor: pointer;"
                        No items to view</div
                </NoRecordsTemplate
                <PagerStyleMode="NumericPages"PageButtonCount="4"/> 
            </MasterTableView
        </telerik:RadGrid
    </div
    </form>

ASPX.VB
Public Class bookmarks 
    Inherits System.Web.UI.Page 
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load 
    
    End Sub 
    Protected Sub RadGrid2_NeedDataSource(ByVal sender As Object, ByVal e As Telerik.Web.UI.GridNeedDataSourceEventArgs) Handles RadGrid2.NeedDataSource 
        RadGrid2.DataSource = getBookMarks 
    End Sub 
    Private ReadOnly Property getBookMarks() As IList(Of project) 
        Get 
            Dim strSQL As String = "SELECT PROJ_NAME FROM [OPI_TIMESHEETS] o left outer join DCSC_PROJECT d on o.project_number = d.PROJ_NO " 
            Dim results As IList(Of project) = New List(Of project)() 
            Using connection As IDbConnection = DbProviderFactories.GetFactory("System.Data.SqlClient").CreateConnection() 
                connection.ConnectionString = ConfigurationManager.ConnectionStrings("projcentral").ConnectionString 
    
                Using command As IDbCommand = connection.CreateCommand() 
                    command.CommandText = strSQL 
    
                    connection.Open() 
                    Try 
                        Dim reader As IDataReader = command.ExecuteReader() 
                        While reader.Read() 
                            Dim name As String = reader.GetValue(reader.GetOrdinal("PROJ_NAME")) 
                            results.Add(New project(name)) 
                        End While 
    
                    Catch ex As SqlException 
                        results.Clear() 
                        'lblmsg.Text = ex.Message 
                    End Try 
                End Using 
            End Using 
            lblMsg.Text = results.Count & " items in list" 
            Return results 
    
        End Get 
    End Property 
    Class project 
    
        Private _projName As String 
    
        Sub New(ByVal name As String) 
    
            _projName = name 
    
        End Sub 
#Region "properties" 
    
        Private ReadOnly Property Name() As String 
            Get 
                Return _projName 
            End Get 
        End Property 
    
#End Region 
    End Class 
    
End Class

1 Answer, 1 is accepted

Sort by
0
Maria Ilieva
Telerik team
answered on 13 Jan 2012, 10:27 AM
Hello Douglas,

Please note that in such scenario you should call connection.Close() and reader.Close in the RadGrid DataBound event. See the online demo below which presents similar approach:
http://demos.telerik.com/aspnet-ajax/grid/examples/programming/binding/defaultcs.aspx

Kind regards,
Maria Ilieva
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now
Tags
Grid
Asked by
Douglas
Top achievements
Rank 1
Answers by
Maria Ilieva
Telerik team
Share this question
or