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

Can't Bind Grid

2 Answers 40 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Web Services
Top achievements
Rank 2
Web Services asked on 15 Mar 2011, 06:57 PM
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 Answers, 1 is accepted

Sort by
0
Maria Ilieva
Telerik team
answered on 18 Mar 2011, 01:07 PM
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

0
Web Services
Top achievements
Rank 2
answered on 18 Mar 2011, 02:37 PM
I ended up selecting the data the way I need it in a view, and then using the demo. Thanks,
Tags
Grid
Asked by
Web Services
Top achievements
Rank 2
Answers by
Maria Ilieva
Telerik team
Web Services
Top achievements
Rank 2
Share this question
or