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

Binding a RadGrid to a List<> collection on a button click

2 Answers 1810 Views
Grid
This is a migrated thread and some comments may be shown as answers.
John
Top achievements
Rank 1
John asked on 09 Jun 2011, 11:19 AM

Hi,

I can easily bind the grid to a List<> collection on Page_Load. However, I'm unable to populate the grid with data on an postback from an user event - for example a button click, and I can't really understand why.

Below is a sample of what I'm trying to do - the result should be the three random rows inserted into the grid, but the grid remains empty. I also can provide a small project sample, but I can't attach it to the post - you only accept gif, jpg files?

ASPX:
<%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="test.aspx.cs" Inherits="WebApplication1.test" %>
 
<%@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %>
<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    <telerik:RadButton ID="RadButton1" runat="server" Text="Load"
        onclick="RadButton1_Click">
    </telerik:RadButton>
    <telerik:RadGrid Width="400px" Height="400px" ID="RadGrid1" runat="server">
    </telerik:RadGrid>
</asp:Content>


Code behind:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
 
namespace WebApplication1
{
    public partial class test : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
 
        }
 
        protected void RadButton1_Click(object sender, EventArgs e)
        {
            List<FillTest> testList = new List<FillTest>();
            testList.Add(new FillTest("wer", "ERterte", "ertert", "ertert"));
            testList.Add(new FillTest("ertert", "dfgdfg", "ertert", "ertert"));
            testList.Add(new FillTest("dgdfg", "rwer", "pfsd", "vfswe"));
            RadGrid1.DataSource = testList;
        }
    }
 
    public class FillTest
    {
        string _testProperty, _test, _description, _code;
 
        public string Code
        {
            get { return _code; }
            set { _code = value; }
        }
        public string Test
        {
            get { return _test; }
            set { _test = value; }
        }
        public string Description
        {
            get { return _description; }
            set { _description = value; }
        }
        public string TestProperty
        {
            get { return _testProperty; }
            set { _testProperty = value; }
        }
 
        public FillTest(string testProperty, string test, string description, string code)
        {
            _testProperty = testProperty;
            _test = test;
            _description = description;
            _code = code;
        }
    }
}

2 Answers, 1 is accepted

Sort by
0
Accepted
Radoslav
Telerik team
answered on 09 Jun 2011, 12:04 PM
Hello John,

When you use simple data-binding you need to call Rebind() method explicitly. So in your case you need to change the RadButton1_Click as following:
protected void RadButton1_Click(object sender, EventArgs e)
    {
        List<FillTest> testList = new List<FillTest>();
        testList.Add(new FillTest("wer", "ERterte", "ertert", "ertert"));
        testList.Add(new FillTest("ertert", "dfgdfg", "ertert", "ertert"));
        testList.Add(new FillTest("dgdfg", "rwer", "pfsd", "vfswe"));
        RadGrid1.DataSource = testList;
        RadGrid1.Rebind();
    }

I hope this helps.

Greetings,
Radoslav
the Telerik team

Browse the vast support resources we have to jump start your development with RadControls for ASP.NET AJAX. See how to integrate our AJAX controls seamlessly in SharePoint 2007/2010 visiting our common SharePoint portal.

0
John
Top achievements
Rank 1
answered on 09 Jun 2011, 12:44 PM

Thanks, problem solved.
Tags
Grid
Asked by
John
Top achievements
Rank 1
Answers by
Radoslav
Telerik team
John
Top achievements
Rank 1
Share this question
or