Search code examples
asp.nettelerikradgrid

Telerik RadGrid not showing any data in ASP.NET


I'm trying to make a really simple grid for some data on an ASP.NET page, but clearly I'm doing something wrong here. First of all, let me show you the grid I have on my clientside:

<asp:Panel runat="server" ID="pnlDoorAccess" Visible="False">
    <asp:UpdatePanel runat="server" ID="upnlDoorAccess" UpdateMode="Conditional" ChildrenAsTriggers="false">
        <ContentTemplate>    
            <tel:RadGrid runat="server" ID="gvDoorAccess" AllowSorting="false" AllowPaging="false"
                CssClass="col-sm-12 noPadding" MasterTableView-CssClass="table table-hover table-header-bg table-striped no-footer tableHeaderBorder"
                OnNeedDataSource="radDoorAccess_NeedDataSource" OnItemDataBound="radDoorAccess_ItemDataBound">
                <MasterTableView AutoGenerateColumns="false" TableLayout="Fixed" Caption="" FilterExpression="" AllowNaturalSort="false" DataKeyNames="Month" NoMasterRecordsText="No records to Display">
                    <Columns>
                        <tel:GridBoundColumn DataField="DoorName" HeaderText="Door Name" UniqueName="DoorName"></tel:GridBoundColumn>
                    </Columns>
                </MasterTableView> 
            </tel:RadGrid>
        </ContentTemplate>
    </asp:UpdatePanel>
</asp:Panel>

So I wanna test the grid by only showing 1 column (DoorName), but so far it's not showing anything. Next is the server side code:

The DoorAccess property will fire up Controller.GetDoorAccess(CurrentUser.Id) when the Object Memorystore is empty and return me a DataTable object which I will return and eventually store into a DataSource property later on.

protected List<DoorAccess> DoorAccess
{
    get
    {
        if (omsDoorAccess.DataItem == null || omsDoorAccess.DataItem.GetType() != typeof(List<Option>)) omsDoorAccess.DataItem = Controller.GetDoorAccess(CurrentUser.Id);

        return (omsDoorAccess.DataItem as List<DoorAccess>);
    }
    set
    {
        omsDoorAccess.DataItem = value;
    }
}

protected void Page_Load(Object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        LoadTab_DoorAccess();
    }
}

private void LoadTab_DoorAccess()
{
    // Future implementation
    ReloadTab_DoorAccess();
}

private void ReloadTab_DoorAccess()
{
    gvDoorAccess.DataBind();

    upnlDoorAccess.DataBind();
    upnlDoorAccess.Update();
}

protected void radDoorAccess_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
{
    (sender as RadGrid).DataSource = DoorAccess; 
    // DoorAccess holds 128 items
}

protected void radDoorAccess_ItemDataBound(object sender, GridItemEventArgs e)
{
    if (e.Item.ItemType != GridItemType.Item && e.Item.ItemType != GridItemType.AlternatingItem)
    {
       // Do stuff in the future
    }
}

So what exactly is going on here? There is probably something obvious I'm missing, but right now I don't see it.

If this question makes no sense, could you show me a block of code that would help me create a simple grid?


Solution

  • I see that you have AutoGeneratedColumns="False", a single column declared with the DataField="DoorName", and DataKeyName="Month".

    When using Advanced DataBinding the datasource you are binding to must match the schema in the RadGrid unless you are using AutoGenerateColumns.

    Without seeing your controller code, I think the issue is with the above Markup. There is no way for the grid to bind the single column with the DataField of "DoorName" to the DoorAccess object using the DataKeyName of "Month"(not knowing where "Month" is coming from). Make sure the DataKeyName property contains a unique value from the DoorAccess object and that the column DataField is equal to any property in the DoorAccess object.

    ASPX:

        <telerik:RadGrid ID="RadGrid1" runat="server" OnNeedDataSource="RadGrid1_NeedDataSource">
             <MasterTableView AutoGenerateColumns="False" DataKeyNames="Id" CommandItemDisplay="Top">
                <Columns>
                    <telerik:GridBoundColumn DataField="Id" UniqueName="MyId" HeaderText="My Id"></telerik:GridBoundColumn>
                    <telerik:GridBoundColumn DataField="Value" UniqueName="Value" HeaderText="My Value"></telerik:GridBoundColumn>
                </Columns>
            </MasterTableView>      
        </telerik:RadGrid>
    

    C#:

    public class MyDataModel
    {
        public int Id { get; set; }
        public string Value { get; set; }
    }
    
    
    protected void RadGrid1_NeedDataSource(object sender, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
    {
       List<MyDataModel> datasource = new List<MyDataModel>();
       for (i = 0; i <= 10; i++) {
       datasource.Add(new MyDataModel {
            Id = i,
            Value = "Value" + i.ToString
        });
       }
    
       ((RadGrid)sender).DataSource = datasource;
    
    }
    

    I would first try with autogenerated columns on to make sure valid data is being returned from the NeedDataSource event, and then make sure your DataKeyNames and DataFields are valid properties. Additionally as @Seano666 said, because your grid is in an UpdatePanel any errors the RadGrid throws due to incorrect formatting will be suppressed.