How to get dropwdown value from the grid view i am adding dropdown dynamically

Posted by Surajemo under ASP.NET on 4/21/2014 | Points: 10 | Views : 447 | Status : [Member] | Replies : 3
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

/// <summary>
/// Summary description for MyGridViewTemplate
/// </summary>
My class file

/// <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 = "<B>" + columnName + "</B>";
// 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;
}
}




Responses

Posted by: Surajemo on: 4/21/2014 [Member] Starter | Points: 25

Up
0
Down
Any My Code Behind file
private void FirstName_DataBinding(Object sender, EventArgs e)
{
// Get the Label control to bind the value. The Label control
// is contained in the object that raised the DataBinding event (the sender parameter).
Label l = (Label)sender;

// Get the GridViewRow object that contains the Label control.
GridViewRow row = (GridViewRow)l.NamingContainer;

// Get the field value from the GridViewRow object and assign it to the Text property of the Label control.
l.Text = DataBinder.Eval(row.DataItem, "Links").ToString();

}

private void LastName_DataBinding(Object sender, EventArgs e)
{
// Get the Label control to bind the value. The Label control
// is contained in the object that raised the DataBinding event (the sender parameter).
Label l = (Label)sender;

// Get the GridViewRow object that contains the Label control.
GridViewRow row = (GridViewRow)l.NamingContainer;

// Get the field value from the GridViewRow object and assign it to the Text property of the Label control.
l.Text = DataBinder.Eval(row.DataItem, "Links1").ToString();
}
}

and her's my.cs file

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 (GridView grow in gvTest.Rows)
{
DropDownList ddl = (DropDownList)grow.FindControl("ddl");
}
}

Surajemo, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: A2H on: 4/22/2014 [Member] [MVP] Silver | Points: 25

Up
0
Down
In above code your are assigning gridviewrow to GridView and thats the reason for this error. You need change the below line from
foreach (GridView grow in gvTest.Rows)

to
foreach (GridViewRow grow in gvTest.Rows)

Complete Code:
protected void ButtonShow(object sender, EventArgs e)
{
//Looping through the rows in GridView
foreach (GridViewRow grow in gvTest.Rows)
{
DropDownList ddl = (DropDownList)grow.FindControl("ddl");
}
}


Thanks,
A2H
My Blog

Surajemo, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Surajemo on: 4/22/2014 [Member] Starter | Points: 25

Up
0
Down
Thank you for Replying But i am getting the same error .

Surajemo, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response