简体   繁体   中英

Telerik RadGrid not displaying records

I am having an issue displaying data in a RadGrid. There is data in the datasource, but the RadGrid does not display it.

aspx code:

<telerik:RadGrid ID="RadGrid1" runat="server" Width="980px" 
EnableEmbeddedSkins="false"
AllowAutomaticDeletes="false" 
AllowAutomaticInserts="false"
AllowAutomaticUpdates="true" 
AllowPaging="true" 
AutoGenerateColumns="False"              
AutoGenerateDeleteColumn="false" 
AutoGenerateEditColumn="false" 
ItemStyle-Height="20px" 
ClientSettings-ActiveRowIndex="true"
EnableViewState = "false" 
OnNeedDataSource = "RadGrid1_NeedDataSource">
<MasterTableView AllowSorting="true" PageSize="10" Width="980px" EnableViewState="true"
    RetrieveAllDataFields="false">
    <NoRecordsTemplate>There is no data available.</NoRecordsTemplate>

    <ExpandCollapseColumn FilterControlAltText="Filter ExpandColumn column"></ExpandCollapseColumn>
    <Columns>                    
        <telerik:GridBoundColumn DataField="TripId" HeaderText="Trip ID" SortExpression="TripID" UniqueName="TripId"  
            SortAscImageUrl="SortAsc.gif" SortDescImageUrl="SortDesc.gif">
        </telerik:GridBoundColumn>
        <telerik:GridBoundColumn DataField="TripLegId" HeaderText="TripLegId" SortExpression="TripLegId" UniqueName="TripLegId" 
            SortAscImageUrl="SortAsc.gif" SortDescImageUrl="SortDesc.gif">
        </telerik:GridBoundColumn>
        <telerik:GridBoundColumn DataField="MemberFirstName" HeaderText="Member First Name" SortExpression="MemberFirstName"
            UniqueName="MemberFirstName" 
            SortAscImageUrl="SortAsc.gif" SortDescImageUrl="SortDesc.gif">
        </telerik:GridBoundColumn>
    </Columns>

    <PagerStyle FirstPageImageUrl="PagingFirst.gif" 
        LastPageImageUrl="PagingLast.gif" NextPageImageUrl="PagingNext.gif" 
        PrevPageImageUrl="PagingPrev.gif" />
    <CommandItemStyle Font-Bold="True" ForeColor="#0066CC" />
</MasterTableView>
<ClientSettings>
    <Selecting AllowRowSelect="true" />
</ClientSettings>
<FilterMenu EnableImageSprites="False"></FilterMenu>            
</telerik:RadGrid>  

.cs code

public void btnSearch_Clicked(object sender, EventArgs e)
{
    try
    {
        RadGrid1.DataSource = GetTripsDataSet();
        RadGrid1.DataBind();
        this.RadGrid1.CurrentPageIndex = 0;
        ViewState["newset"] = "new";
    }
    catch (Exception ex)
    {
        string errMessage = ex.Source.ToString();
    }
}  

protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
{
    if (ViewState["newset"] == null) return;

    if (Session["gridTrips"] != null)
    {
        DataTable dt = (DataTable)Session["griTrips"];
        if (dt.Rows.Count > 0)
        {
            this.RadGrid1.DataSource = dt;
        }
    }
}

You just need to rebind the grid, which will automatically call the grid's NeedDataSource event.

public void btnSearch_Clicked(object sender, EventArgs e)
{
    try
    {
        this.RadGrid1.Rebind();
        this.RadGrid1.CurrentPageIndex = 0;
        ViewState["newset"] = "new";
    }
    catch (Exception ex)
    {
        string errMessage = ex.Source.ToString();
    }
}  

protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
{
    if (ViewState["newset"] == null) return;

    if (Session["gridTrips"] != null)
    {
        DataTable dt = (DataTable)Session["griTrips"];
        if (dt.Rows.Count > 0)
        {
            this.RadGrid1.DataSource = dt;
            this.RadGrid1.VirtualItemCount = dt.Rows.Count;
        }
    }
}

It's also good to set the grid's VirtualItemCount property, especially if you're using custom paging. From the Telerik RadGrid metadata on this property:

Summary: Gets or sets a value, indicating the total number of items in the data source when custom paging is used. Thus the grid "understands" that the data source contains the specified number of records and it should fetch merely part of them at a time to execute requested operation.

Remarks: If you set a value that is greater than the actual number of items, RadGrid will show all available items plus empty pages (or whatever other content you set) for the items that exceed the actual number. For example you have a data source with 9'000 items and you set VirtualItemCount to 10'000. If your page size is 1000, the grid will render 10 pages and the last page will be empty (or with NoRecordsTemplate if you're using such).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM