Click here to Skip to main content
15,885,757 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Good Day,

Hi

I have a grid i am creating template fields dynamically from code behind and i am adding dropdown dynamically it is being added sucessfully

the gridview is being show problemis when i try to get the dropwdown value from grid from looping it gives me error

Unable to cast object of type 'System.Web.UI.WebControls.GridViewRow' to type 'System.Web.UI.WebControls.GridView'.

i am looping throgh the grid view on a button click hers my code

My class file

C#
/// <summary>
/// Summary description for MyGridViewTemplate
/// </summary>
public class MyGridViewTemplate : ITemplate
{
private DataControlRowType templateType;
private string columnName;

public MyGridViewTemplate(DataControlRowType type, string colname)
{
templateType = type;
columnName = colname;
}

public void InstantiateIn(System.Web.UI.Control container)
{
// Create the content for the different row types.
switch (templateType)
{
case DataControlRowType.Header:
// Create the controls to put in the header section and set their properties.
Literal lc = new Literal();
lc.Text = "" + columnName + "";
// Add the controls to the Controls collection of the container.
container.Controls.Add(lc);
break;
case DataControlRowType.DataRow:
// Create the controls to put in a data row section and set their properties.
DropDownList firstName = new DropDownList();
firstName.ID = "ddl";
Label lastName = new Label();
for (int i = 0; i < 2; i++)
{
firstName.Items.Add(new ListItem(i.ToString(), i.ToString()));

}
// Add the controls to the Controls collection of the container.
container.Controls.Add(firstName);

break;

// Insert cases to create the content for the other row types, if desired.

default:
// Insert code to handle unexpected values.
break;
}
}


and her's my.cs file
C#
protected void Page_Load(object sender, EventArgs e)
{

////Populate Data
DataTable dt = new DataTable();
dt.Columns.Add("Links");
dt.Columns.Add("Links1");
DataRow dr;
for (int i = 1; i < 10; i++)
{
dr = dt.NewRow();
dr["Links"] = "A" + i;
dr["Links1"] = "B" + i;
dt.Rows.Add(dr);
dt.AcceptChanges();
}
////Grid view
// The field columns need to be created only when the page is first loaded. 
if (!IsPostBack)
{
gvTest.AutoGenerateColumns = false;
// Dynamically create field columns to display the desired
// fields from the data source. Create a TemplateField object 
// to display an author's first and last name.
TemplateField customField = new TemplateField();
// Create the dynamic templates and assign them to 
// the appropriate template property.
for (int i = 0; i < 10; i++)
{
customField = new TemplateField();
customField.HeaderTemplate = new MyGridViewTemplate(DataControlRowType.Header,i.ToString());
customField.ItemTemplate = new MyGridViewTemplate(DataControlRowType.DataRow,i.ToString());
// Add the field column to the Columns collection of the GridView control.
this.gvTest.Columns.Add(customField);
}

// Add the field column to the Columns collection of the GridView control.
this.gvTest.Columns.Add(customField);
BoundField boundField = new BoundField();
boundField.HeaderText = "Bug ID";
boundField.DataField = "Links";
this.gvTest.Columns.Add(boundField);

gvTest.DataSource = dt;
gvTest.DataBind();

}
}
protected void ButtonShow(object sender, EventArgs e)
{


foreach (GridViewRow grow in gvTest.Rows)
{
DropDownList ddl = (DropDownList)grow.FindControl("ddl");
}
}




I am getting Error on this line
DropDownList ddl = (DropDownList)grow.FindControl("ddl");
Posted
Updated 21-Apr-14 19:52pm
v4
Comments
Ankur\m/ 22-Apr-14 1:52am    
Every other day there is a question asking accessing dynamic controls in GridView. Did you search the web? There are so many discussion for the same out there.
surajemo 22-Apr-14 2:11am    
Yes Sir
i am creating template fields dynamically and then adding dropdown dynamically
but i am not able to get the dropdown's when i loop them
foreach (GridViewRow grow in gvTest.Rows)
{
DropDownList ddl = (DropDownList)grow.FindControl("ddl");
}
}
Sunasara Imdadhusen 22-Apr-14 7:00am    
can you please send your Page_Load code??

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900