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

Using user control in radgrid

3 Answers 286 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Joe
Top achievements
Rank 1
Joe asked on 15 Feb 2011, 07:15 PM
I am trying to insert a user control into an item template of a radgrid to display data dynamically, but I keep getting an object reference not set to an instance of an object error when it tries to set the property container.

Below is a snippet of where I am using the user control in the radgrid:
<%@ Register Src="PropertyContainer.ascx" TagName="PropertyContainer" TagPrefix="uc1" %>
  
  
<radG:RadGrid ID="employees" OnNeedDataSource="EmployeesGrid_NeedDataSource" runat="server" AutoGenerateColumns="false">
     <Columns>
          <radG:GridTemplateColumn>
              <ItemTemplate>
                  <uc1:PropertyContainer ID="PropertyContainer" runat="server" />
              </ItemTemplate>
          </radG:GridTemplateColumn
      </Columns>

Below is a snippet of the code behind:
protected void Page_Load(object sender, EventArgs e)
{
              
}
  
public void EmployeesGrid_NeedDataSource(object source, Telerik.WebControls.GridNeedDataSourceEventArgs e)
{
     foreach (Employee emp in Employees)
     {
           PropertyContainer.SetProperties(emp.Properties);
     }
)

The employee's properties are not null, and the fields will not be editable.

Any assistance would be appreciated.  Thanks.

Joe

3 Answers, 1 is accepted

Sort by
0
Accepted
Shinu
Top achievements
Rank 2
answered on 16 Feb 2011, 09:19 AM
Hello Joe,

You can try the following approach to achieve the same.
  • Set the DataSource in the NeedDataSource Event.
  • In the ItemDataBound access the UserControl and set the property.
Here is a Sample code.
protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
{
RadGrid1.DataSource = Employees;
}
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
{
    if (e.Item is GridDataItem)
    {
        GridDataItem item = (GridDataItem)e.Item;
        Employee emp = (Employee )item.DataItem;
        UserControl_PropertyContainer uc = (UserControl_PropertyContainer )item.FindControl(PropertyContainer");
        uc.setProperties(emp.Properties);
    }
}


Thanks,
Shinu.
0
Joe
Top achievements
Rank 1
answered on 17 Feb 2011, 03:06 AM
Thank you Shinu for your reply; unfortunately that did not work.

The user control is still null giving me the object reference not set to an instance of an object error.  Do you have any other suggestions to try?

Thanks
Joe
0
Tsvetina
Telerik team
answered on 23 Feb 2011, 08:24 AM
Hello Joe,

Please, paste the code that you use to access the user control on ItemDataBound. It should be available there. In the meantime, please, take a look at this help article dealing with accessing the items in a RadGrid:
Accessing cells and rows

Greetings,
Tsvetina
the Telerik team
Registration for Q1 2011 What’s New Webinar Week is now open. Mark your calendar for the week starting March 21st and book your seat for a walk through all the exciting stuff we ship with the new release!
Tags
Grid
Asked by
Joe
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Joe
Top achievements
Rank 1
Tsvetina
Telerik team
Share this question
or