RadGrid by Programatically data bind

4 posts, 0 answers
  1. Syed
    Syed avatar
    36 posts
    Member since:
    Sep 2013

    Posted 08 Oct 2013 Link to this post

    hi,
    Iam using Radgrid for my ASP.Net project.
    I want to design,bound ,bind and create events for my grid by program.
    for ex in my design i will have

    <telerik:radgrid ID="RadGrid1" runat="server" >

                  </telerik:radgrid>


    In page init i define all bound columns and design of grid and create a event for the grid for ex.
     protected void Page_Init(object source, System.EventArgs e)
        {
            Loadprogramaticgrid();
            RadGrid1.NeedDataSource += new GridNeedDataSourceEventHandler(RadGrid1_NeedDataSource);
        }

      private void Loadprogramaticgrid()
        {
                 RadGrid1.MasterTableView.DataKeyNames = new string[] { "username" };
            RadGrid1.Skin = "Default";
            RadGrid1.Width = Unit.Percentage(100);
                  RadGrid1.AutoGenerateColumns = false;
            //Add columns
            GridBoundColumn boundColumn;
            boundColumn = new GridBoundColumn();
            boundColumn.DataField = "username";
            boundColumn.HeaderText = "username";
            RadGrid1.MasterTableView.Columns.Add(boundColumn);
            boundColumn = new GridBoundColumn();
            boundColumn.DataField = "firstname";
            boundColumn.HeaderText = "firstname";
            RadGrid1.MasterTableView.Columns.Add(boundColumn);

            boundColumn = new GridBoundColumn();
            boundColumn.DataField = "lastname";
            boundColumn.HeaderText = "lastname";
            RadGrid1.MasterTableView.Columns.Add(boundColumn);
         
        }
     protected void RadGrid1_NeedDataSource(object sender, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
        {
            RadGrid1.DataSource = loaddata();
        }
    and i have a Button and in button click i rebind the grid as
     protected void Button1_Click(object sender, EventArgs e)
        {
            RadGrid1.Rebind();
        }

    now the problem is for first time the grid binds and display the data in grid.
    When i click the button the grid is not binding correctly.
    i have attached the image for more referance.

  2. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 08 Oct 2013 Link to this post

    Hi Syed,

    Please create the entire RadGrid from code behind,to avoid such issues.

    ASPX:
    <asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>
    <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click1" />

    C#:
      RadGrid RadGrid1;   
       protected void Page_Init(object source, System.EventArgs e)
       {
           Loadprogramaticgrid();
           RadGrid1.NeedDataSource += new GridNeedDataSourceEventHandler(RadGrid1_NeedDataSource);
       }
     
     private void Loadprogramaticgrid()
       {
            RadGrid1  = new RadGrid();
            RadGrid1.ID = "RadGrid1";
            RadGrid1.MasterTableView.DataKeyNames = new string[] { username};
            RadGrid1.Skin = "Default";
            RadGrid1.Width = Unit.Percentage(100);
            RadGrid1.AutoGenerateColumns = false;
            RadGrid1.AllowPaging = true;
            
           GridBoundColumn boundColumn;
           boundColumn = new GridBoundColumn();
           RadGrid1.MasterTableView.Columns.Add(boundColumn);
           boundColumn.DataField = "username";
           boundColumn.HeaderText = "username";
           
           boundColumn = new GridBoundColumn();
           RadGrid1.MasterTableView.Columns.Add(boundColumn);
          boundColumn.DataField = "firstname";
           boundColumn.HeaderText = "firstname";
           
     
           boundColumn = new GridBoundColumn();
           RadGrid1.MasterTableView.Columns.Add(boundColumn);
             boundColumn.DataField = "lastname";
           boundColumn.HeaderText = "lastname";
     
           this.PlaceHolder1.Controls.Add(RadGrid1);
         
       }
    protected void RadGrid1_NeedDataSource(object sender, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
       {
           RadGrid1.DataSource = loaddata();
       }
    protected void Button1_Click1(object sender, EventArgs e)
        {
        RadGrid1.Rebind();
        }

    Thanks,
    Princy
  3. Syed
    Syed avatar
    36 posts
    Member since:
    Sep 2013

    Posted 08 Oct 2013 Link to this post

    Hi,
    Thanks.it works
  4. Naga
    Naga avatar
    1 posts
    Member since:
    Feb 2015

    Posted 14 Dec 2016 Link to this post

    can u send sample code for populating data on telerikgrid based on button click??

Back to Top