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

Displaying the same grid on two different pages

1 Answer 17 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Chuck
Top achievements
Rank 1
Veteran
Chuck asked on 25 Jan 2021, 01:26 PM
I have a RadGrid being populated and displaying but the client needs a condition format for the grid to display new columns while hiding others. Is there a way to display the same grid on two different pages with different data source?

1 Answer, 1 is accepted

Sort by
0
Attila Antal
Telerik team
answered on 24 Feb 2021, 11:55 AM

Hi Chuck,

Yes, you can create a WebUserControl with a RadGrid in it. From there, you have various options depending on the scenario, however, I will show you one example below.

WebUserControl.ascx

Create a WebUserControl with RadGrid. Do not declare any columns as the DataSource would change dynamically. Let the Grid render the Columns from the data source bound to it.

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="WebUserControl.ascx.cs" Inherits="WebUserControl" %>

<telerik:RadGrid ID="RadGrid1" runat="server" RenderMode="Lightweight" OnNeedDataSource="RadGrid1_NeedDataSource">
</telerik:RadGrid>

 

WebUserControl.ascx.cs

Create a Public property that you can access from outside of the Control. E.g. when declaring/loading it.

using Telerik.Web.UI;

public partial class WebUserControl : System.Web.UI.UserControl
{
    public string MyProperty { get; set; }

    protected void RadGrid1_NeedDataSource(object sender, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
    {
        var grid = (RadGrid)sender;

        switch (MyProperty)
        {
            case "Customer":
                grid.DataSource = GetCustomersTable();
                break;
            case "Order":
                grid.DataSource = GetOrdersTable();
                break;
            default:
                grid.DataSource = GetTheDefaultTable();
                break;
        }
    }
}

 

Customers.aspx

Declare the UserControl on the Customers page and pass the value Customer to the Public Property called "MyProperty"

<uc1:WebUserControl runat="server" ID="WebUserControl1" MyProperty="Customer" />

 

Orders.aspx

Declare the UserControl on the Orders page and pass the value Order to the Public Property called "MyProperty"

<uc1:WebUserControl runat="server" ID="WebUserControl1" MyProperty="Order" />


SomePage.aspx - Other/Random Page with no predefined property (to bind the Default Table)

Add the UserControl without passing any parameter. In this case, the Grid will bind the Default data/table.

<uc1:WebUserControl runat="server" ID="WebUserControl1" />

 

There are other ways as well, but as a base idea, you can share some information with the user control, and bind data conditionally.

 

Regards,
Attila Antal
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

Tags
Grid
Asked by
Chuck
Top achievements
Rank 1
Veteran
Answers by
Attila Antal
Telerik team
Share this question
or