我添加了与存储在会话变量中的csv文件数据绑定的gridview,如下所示:
dgData.DataSource = Session["csvdata"];
dgData.DataBind();
它绑定得很完美。但是我需要在gridview的第一行添加dropdownlist,用于使用网格标题映射数据库列名。我使用此代码添加dropdownlist.But下拉列表添加到标题而不是第一行。
protected void dgData_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header )
{
for (Int32 i = 0; i < e.Row.Cells.Count; i++)
{
DropDownList ddl = new DropDownList();
ddl.ID = "ddlCol" + i.ToString ();
e.Row.Cells[i].Controls.Add(ddl);
}
}
}
答案 0 :(得分:0)
protected void dgData_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DropDownList ddl;
if (e.Row.RowIndex == 0)
{
for (Int32 i = 0; i < e.Row.Cells.Count; i++)
{
ddl = new DropDownList();
ddl.ID = "ddlCol" + i.ToString ();
e.Row.Cells[i].Controls.Add(ddl);
}
}
}
}
此代码将控件添加到网格视图的第一行