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

RadGrid by Programatically data bind

3 Answers 242 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Syed
Top achievements
Rank 1
Syed asked on 08 Oct 2013, 06:26 AM
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.

3 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 08 Oct 2013, 08:03 AM
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
0
Syed
Top achievements
Rank 1
answered on 08 Oct 2013, 08:42 AM
Hi,
Thanks.it works
0
Naga
Top achievements
Rank 1
answered on 14 Dec 2016, 04:11 PM

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

Tags
Grid
Asked by
Syed
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Syed
Top achievements
Rank 1
Naga
Top achievements
Rank 1
Share this question
or