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

AutogenerateColumns causing no data to be displayed

3 Answers 313 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Martin
Top achievements
Rank 1
Martin asked on 27 Mar 2012, 06:15 PM
Hello,
I have a RadGrid inside a Webpart.  Its a non-visual webpart.
I am binding the grid to a Datasource of List<T>.
Every column is being returned from the datasource so I turned off AutoGenerateColumns to remove the extra columns and control what is painted on the screen.

Now the grid always displays "No records to display"...

The radgrid is created in the CreateChildControls override event:
radGridViewSearch = new RadGrid();
           radGridViewSearch.AutoGenerateColumns = false;
           radGridViewSearch.AllowPaging = true;
           radGridViewSearch.PageSize = 25;
           radGridViewSearch.NeedDataSource += new GridNeedDataSourceEventHandler(radGridViewSearch_NeedDataSource);
           this.Controls.Add(radGridViewSearch);


I am adding only one column to see if I can remove the possible reasons for the error:
GridBoundColumn colFacilityID = new GridBoundColumn();
           colFacilityID.DataField = "Facility";
           colFacilityID.HeaderText = "Facility";
           radGridViewSearch.Columns.Add(colFacilityID);


And the NeedDataSource event is used:
void radGridViewSearch_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
        {           
            lionRepository = new LionBCSRepository();
            radGridViewSearch.DataSource = lionRepository.BuildMatterDataSet(txtMatterNameSearch.Text.Trim(), txtContractNameSearch.Text.Trim(), txtPhysicianNameSearch.Text.Trim());
        }


I can see that 40+ records are returned, but its not binding them - is this some strange page life cycle issue?


Please help

3 Answers, 1 is accepted

Sort by
0
Martin
Top achievements
Rank 1
answered on 27 Mar 2012, 07:47 PM
ok - I've simplified the code and now the NeedDataSource event is never being fired.
I've poured over the
KB articles here and postings - trying to follow as closely as possible...

If we can't get this to work very quickly then we'll need to abandon Telerik and go with the Standard GridView instead:
Again, this Datasource returns a List<T>, but its never being called, so I don't think that is the issue.

Code:
RadGrid radGridViewSearch;
LionBCSRepository lionRepository;
 
protected override void CreateChildControls()
{          
    radGridViewSearch = new Telerik.Web.UI.RadGrid();           
    this.Controls.Add(radGridViewSearch);
    radGridViewSearch.NeedDataSource += new GridNeedDataSourceEventHandler(radGridViewSearch_NeedDataSource);           
                 
    GridBoundColumn colFacilityID = new GridBoundColumn();
    radGridViewSearch.Columns.Add(colFacilityID);    
    colFacilityID.DataField = "Facility";
    colFacilityID.HeaderText = "Facility";           
}
 
 
void radGridViewSearch_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
{
    lionRepository = new LionBCSRepository();
    radGridViewSearch.DataSource = lionRepository.BuildMatterDataSet(string.Empty, string.Empty, string.Empty);
}
 
protected override void RenderContents(HtmlTextWriter writer)
{           
    radGridViewSearch.RenderControl(writer);           
}

0
Martin
Top achievements
Rank 1
answered on 27 Mar 2012, 10:07 PM
ok - using this simplified code and adding a Rebind() method to the RenderContents method I seem to have the ability to bind.
But...
When I add Paging the control blows up again....

The error is that "Script controls may not be registered after PreRender."

beginning to truly not enjoy these controls from you guys...

code below as it stands
#region Fields
         
        RadGrid radGridViewSearch;       
        LionBCSRepository lionRepository;
 
        #endregion
 
        protected override void CreateChildControls()
        {
            radGridViewSearch = new Telerik.Web.UI.RadGrid();
            //radGridViewSearch.MasterTableView.DataKeyNames = new string[] { "Facility" };
            radGridViewSearch.MasterTableView.AutoGenerateColumns = false;
            radGridViewSearch.MasterTableView.AllowPaging = true;
            radGridViewSearch.MasterTableView.PageSize = 25;
            radGridViewSearch.NeedDataSource += new GridNeedDataSourceEventHandler(radGridViewSearch_NeedDataSource);
            this.Controls.Add(radGridViewSearch);
 
            GridBoundColumn boundColumn;
            boundColumn = new GridBoundColumn();
            radGridViewSearch.MasterTableView.Columns.Add(boundColumn);
            boundColumn.UniqueName = "Facility";
            boundColumn.DataField = "Facility";
            boundColumn.HeaderText = "Facilities";
        }
 
 
        void radGridViewSearch_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
        {
            lionRepository = new LionBCSRepository();
            radGridViewSearch.DataSource = lionRepository.BuildMatterDataSet(string.Empty, string.Empty, string.Empty);
        }
 
        /// <summary>
        /// Using RenderContents For greater control of the layout on the page
        /// </summary>
        /// <param name="writer"></param>
        protected override void RenderContents(HtmlTextWriter writer)
        {
            radGridViewSearch.Rebind();
            radGridViewSearch.RenderControl(writer);           
        }

0
Accepted
Marin
Telerik team
answered on 30 Mar 2012, 03:04 PM
Hello Martin,

 We have addressed this question in the support ticket that you have opened. If you do not mind we will continue further communication there in order to avoid duplicate posts.

For the benefit of the community I am also posting the answer here:

When creating the grid in code-behind you have to add the columns before the grid is added to the Controls collection of the WebPart - it should be the last line in the CreateChildControls method: 

protected override void CreateChildControls()
        {
            radGridViewSearch = new Telerik.Web.UI.RadGrid();
            radGridViewSearch.AutoGenerateColumns = false;
            radGridViewSearch.MasterTableView.AllowPaging = true;
            radGridViewSearch.MasterTableView.PageSize = 25;
            radGridViewSearch.NeedDataSource += new GridNeedDataSourceEventHandler(radGridViewSearch_NeedDataSource);
            //add any additional settings that you need here
             
            GridBoundColumn boundColumn;
            boundColumn = new GridBoundColumn();
            radGridViewSearch.MasterTableView.Columns.Add(boundColumn);
            boundColumn.UniqueName = "Facility";
            boundColumn.DataField = "Facility";
            boundColumn.HeaderText = "Facilities";
 
            boundColumn = new GridBoundColumn();
            radGridViewSearch.MasterTableView.Columns.Add(boundColumn);
            boundColumn.UniqueName = "Location";
            boundColumn.DataField = "Location";
            boundColumn.HeaderText = "Locations";
             
            this.Controls.Add(radGridViewSearch);
        }

Also you do not need to call explicitly RenderControl of the grid since it is already added to the controls collection and will render properly.
For further reference I am attaching a sample test web part that demonstrates the above approach, feel free to check it on your side. 


Regards,
Marin
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
Martin
Top achievements
Rank 1
Answers by
Martin
Top achievements
Rank 1
Marin
Telerik team
Share this question
or