I created a RadGrid programmatically and added the RadGrid to a PlaceHolder control. The PlaceHolderControl is in a RadPageView.
In the code-behind, how can I programmatically find the RadGrid control when I click on a button?
I have tried the following code but to no avail.
protected void GetVotingsBtn_Click(object sender, EventArgs e)
{
RadGrid vGrid = null;
PlaceHolder GridPlaceHolder = (PlaceHolder)this.VotingRadPageView.FindControl("GridPlaceHolder");
if (GridPlaceHolder != null) { vGrid = (RadGrid)this.GridPlaceHolder.FindControl("VRadGrid"); }
}
The value of the vGrid is always null. Please, help.
Thanks in advance.
4 Answers, 1 is accepted

I believe your code will work if you use the exact ID of the control while accessing it. Have you missed setting the ID property for the grid when creating it?
C#:
protected
void
Page_Init(
object
source, System.EventArgs e)
{
RadGrid RadGrid1 =
new
RadGrid();
RadGrid1.ID =
"RadGrid1"
;
RadGrid1.DataSourceID =
"SqlDataSource1"
;
}
And when accessing use the same ID as shown below:
C#:
protected
void
Button1_Click(
object
sender, EventArgs e)
{
RadGrid Grid1 = (RadGrid)PlaceHolder1.FindControl(
"RadGrid1"
);
}
Could you paste the code that you tried for adding the grid control, if that is different from this?
Thanks,
Princy.

1) In the first RadPageView, I have two listbox. I populate the left listbox with items from the database. I then select items from the left listbox and drop them in the right listbox. The selected items in the right listbox will be used as columns when dynamically building my RadGrid.
2) In this step, I will click on a "Next" button and upon doing so, I call a "DefineGridStructure" method that dynamically builds my RadGrid and displays the Grid in the second RadPageView. Each column in this RadGrid contains RadNumericTextBox that I will need to find and obtain any data entered into them. Now, in this second RadPageView, I also have a button such that when I click on this button (GetValues), I want to be able to find controls in my dynamically built grid and obtain their values. So I think what happens here is that once I click on the "GetValues" button, there is a postback and the dynamically built Grid is gone.
So I am wondering if I build this Grid in a UserControl, then insert the UserControl in the PlaceHolder control found in the second RadPageView will help in anyway? Any other approach here will be helpful. By the way below is the code I use to build the Grid.
private
void DefineGridStructure()
{
RadGrid VotingRadGrid = new RadGrid();
VotingRadGrid.DataSourceID =
"ImpactCatDataSource";
VotingRadGrid.MasterTableView.DataKeyNames =
new string[] {"ImpactCategoryId"};
VotingRadGrid.Skin =
"Web20"; // "Office2007";
VotingRadGrid.GridLines =
GridLines.Both;
VotingRadGrid.ID =
"VRadGrid";
VotingRadGrid.AutoGenerateColumns =
false;
/Add columns
this.AddImpactCategoryToGrid(VotingRadGrid);//Impact Category Column
this.AddRiskToGrid(VotingRadGrid);//Risk Columns
this.AddComponentToGridStructure(VotingRadGrid);//Add columns with RadNumericTextBox
this.GridPlaceHolder.Controls.Add(VotingRadGrid);
}
Thanks
Jeff

Has anyone been able to find a solution on how to capture data in controls found in a dynamically created RadGrid or Datagrid?
Thanks in advance.
Jeff.

Merci!
Jeff