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

Update RadGrid DataSource from external class

1 Answer 96 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Rocio
Top achievements
Rank 1
Rocio asked on 16 Nov 2012, 10:16 AM
Hello! I am using Telerik ASP.NET controls to develop a WebApplication in C#, What I need is to show some data in a Grid, so I selected RadGrid to do it.
In my firt test, I just have a textbox with a button, so when you click on it, the text on the textbox is added to a global list, and this list is copied to a local list and added as DataSource:

public static List<string> gl = new List<string>(); //Global list to store all the elements
protected void RadButton3_Click(object sender, EventArgs e)
    {
        string txt = RadTextBox1.Text;
        UpdateGrid(txt);      
    }
 
public void UpdateGrid(string elem) //Procedure to update the grid contents
    {
        gl.Add(elem);
        List<string> l = new List<string>(); //Local list to set it as DataSource for the grid, so I don't get the exception
        l.AddRange(gl);
        RadGrid1.DataSource = l;
        RadGrid1.DataBind();
    }

I tested it and all worked fine, the problem is that the elements that I have to add to the grid are not those from the textbox, but they come from a WCF Duplex Service. So I add the Service Reference and create the client object to invoke the Service functions. As it is a Duplex Service, the client must implement Callback interface with the function that will be executed when the service returns a value.

I added a new button, to call the service, the Callback class, and a static class "UIGlobal" so I could access to the UpdateGrid method from this Callback class and pass the value I want to add to the grid:

public partial class Default : System.Web.UI.Page
{
    public static List<string> gl = new List<string>();
    protected void Page_Load(object sender, EventArgs e)
    {
        UIGlobal.MainPage = this; //Initialize the attribute of UIGlobal to get the reference os this page to access the UpdateGrid method
    }
 
    protected void RadButton3_Click(object sender, EventArgs e) //Same as before
    {
        string txt = RadTextBox1.Text;
        UpdateGrid(txt);      
    }
 
    public void UpdateGrid(string elem) //The method that updates the grid
    {
        gl.Add(elem);
        List<string> l = new List<string>();
        l.AddRange(gl);
        RadGrid1.DataSource = l;
        RadGrid1.DataBind();
    }
 
    protected void RadButton2_Click(object sender, EventArgs e) //Method that calls the WCF Duplex Service
    {
        InstanceContext instanceContext = new InstanceContext(new CallbackHandler());
        PubSubClient ps_service = new PubSubClient(instanceContext); //Create the object required to call the service
 
        List<string> tops = new List<string>();
        tops.Add("realtime.db.analog.0analog.description");
        ps_service.subscribe_Integrity(tops); //Call the service function
    }
}
 
public class CallbackHandler : IPubSubCallback //This is the Callback class
{
    public void Send(Change c) //When the service sends a value to the client, this function is executed
    {
        UIGlobal.MainPage.UpdateGrid(c.value); //Execute the UpdateGrid method with the value that the service sends back
    }
}
 
public class UIGlobal //This is the class used to access to the page
{
    public static Default MainPage { get; set; } //Reference to the page, initialized  in the Page_Load method
}

When I execute it, everything seems to work fine, there are not exceptions thrown and I can see how the Send function is executed, it calls the UpdateGrid with the right value, and this function execute all the instructions, but the new value is not added to the grid, so I don't know what is happening, I don't know if it is because the function is being called from the UIGloblal Reference.
I can send you my proyect with both the asp.net client and the duplex service if you want.

Hope somebody can help me. Thanks!!


1 Answer, 1 is accepted

Sort by
0
Andrey
Telerik team
answered on 21 Nov 2012, 07:24 AM
Hi,

You could try to use NeedDataSource event instead of using DataBind method to bind the Grid. Your code will remain the same, you just need to remove the DataBind call and place the other binding logic in the NeedDataSource event.

Give this suggestion a try and check whether you need further assistance.

Greetings,
Andrey
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
Tags
Grid
Asked by
Rocio
Top achievements
Rank 1
Answers by
Andrey
Telerik team
Share this question
or