本文介绍了如何从网格视图中获取下拉值我正在动态添加下拉列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

美好的一天,







我有一个网格我正在动态创建模板字段代码背后,我正在动态添加下拉列表它正在成功添加



当我尝试从循环中获取网格的dropwdown值时,gridview正在显示问题它给了我错误



无法将'System.Web.UI.WebControls.GridViewRow'类型的对象强制转换为'System.Web.UI.WebControls.GridView'。



i我循环播放按钮上的网格视图点击她的代码



我的班级文件



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>
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;
}
}





和她的my.cs文件



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









我我在这一行得到错误

DropDownList ddl =(DropDownList)grow.FindControl(ddl);





I am getting Error on this line
DropDownList ddl = (DropDownList)grow.FindControl("ddl");

推荐答案

这篇关于如何从网格视图中获取下拉值我正在动态添加下拉列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-16 13:51