Can't Bind Grid

3 posts, 0 answers
  1. Web Services
    Web Services avatar
    243 posts
    Member since:
    Apr 2008

    Posted 15 Mar 2011 Link to this post

    I'm trying to bind a rad grid from a custom datatable but I cannot get any of the data to show. I have read all your examples but still cannot seem to get it to work. Here's my code
    <telerik:RadGrid ID="RadGrid1" runat="server" AllowSorting="True"
    GridLines="None" ShowGroupPanel="True" AutoGenerateColumns="False" OnNeedDataSource="RadGrid1_NeedDataSource">
        <MasterTableView GridLines="None" Width="100%" CommandItemDisplay="Top">
            <Columns>
                <telerik:gridboundcolumn datafield="FirstName" headertext="First Name"
                </telerik:gridboundcolumn>
                <telerik:gridboundcolumn datafield="LastName" headertext="Last Name"
                </telerik:gridboundcolumn>
                <telerik:gridboundcolumn datafield="Business Name" headertext="Business Name"
                </telerik:gridboundcolumn>
                <telerik:gridboundcolumn datafield="State" headertext="State"
                </telerik:gridboundcolumn>
                <telerik:gridboundcolumn datafield="Zip" headertext="Zip"
                </telerik:gridboundcolumn>
                <telerik:gridboundcolumn datafield="Address" headertext="Address"
                </telerik:gridboundcolumn>
                <telerik:gridboundcolumn datafield="City" headertext="City"
                </telerik:gridboundcolumn>
                <telerik:gridboundcolumn datafield="Phone" headertext="Phone"
                </telerik:gridboundcolumn>
                <telerik:gridboundcolumn datafield="Fax" headertext="Fax"
                </telerik:gridboundcolumn>
                <telerik:gridboundcolumn datafield="CellPhone" headertext="Cell"
                </telerik:gridboundcolumn>
                <telerik:gridboundcolumn datafield="Eamil" headertext="Email"
                </telerik:gridboundcolumn>
                <telerik:gridboundcolumn datafield="Edit" headertext="Edit"
                </telerik:gridboundcolumn>
            </Columns>
        </MasterTableView>
        <ClientSettings AllowDragToGroup="True">
            <Scrolling AllowScroll="True" UseStaticHeaders="True" />
        </ClientSettings>
    </telerik:RadGrid>

    VB
    'this function gets the data for the rad grid
    Private Function getData()
        Dim table As DataTable = New DataTable()
        table.Columns.Add("First Name")
        table.Columns.Add("Last Name")
        table.Columns.Add("Business Name")
        table.Columns.Add("State")
        table.Columns.Add("Zip")
        table.Columns.Add("Address")
        table.Columns.Add("City")
        table.Columns.Add("Phone")
        table.Columns.Add("Fax")
        table.Columns.Add("Email")
        table.Columns.Add("CellPhone")
        table.Columns.Add("Edit")
        'my sql connection
        Dim myConn As New Data.SqlClient.SqlConnection(conn)
     
        'the name of the stored procedure
        Dim strSQL = "getAllCustomers"
     
        'add the initial - Please Select -
        'table.Rows.Add(New String() {"-1", "- Please Select -"})
     
        Try
     
            myConn.Open()
            Dim readCommand As New Data.SqlClient.SqlCommand(strSQL, myConn)
     
            'while we have rows from the stored procedure
            'we will add them to a data table
            Dim cdr As SqlDataReader = readCommand.ExecuteReader()
            While cdr.Read()
     
                If (cdr.Item(0).ToString <> "") Then
     
                    table.Rows.Add(New String() {cdr.Item(1), cdr.Item(2), cdr.Item(3), cdr.Item(4), cdr.Item(5), cdr.Item(6), cdr.Item(7), cdr.Item(8), cdr.Item(9), cdr.Item(10), cdr.Item("<a href='EditCustomer.aspx?id=" & cdr.Item(0) & "'>Edit</a>")})
     
                End If
     
            End While 'while cdr.Read()
     
        Catch ex As Exception
     
        End Try
        myConn.Close()
     
        Return table
     
    End Function 'createTable
     
     
    Protected Sub RadGrid1_NeedDataSource(ByVal source As Object, ByVal e As Telerik.Web.UI.GridNeedDataSourceEventArgs) Handles RadGrid1.NeedDataSource
        RadGrid1.DataSource = getData()
    End Sub

  2. Maria Ilieva
    Admin
    Maria Ilieva avatar
    4017 posts

    Posted 18 Mar 2011 Link to this post

    Hello,

    I would suggest you to follow the code provided in the following online demo in order to bind the RadGrid correctly.


    Kind regards,
    Maria Ilieva
    the Telerik team

  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Web Services
    Web Services avatar
    243 posts
    Member since:
    Apr 2008

    Posted 18 Mar 2011 Link to this post

    I ended up selecting the data the way I need it in a view, and then using the demo. Thanks,
Back to Top