将动态DropDownList添加到Gridview(然后导出到excel)

时间:2014-04-07 09:35:04

标签: c# asp.net excel gridview

我从后面的代码创建一个动态的DropDownlist:

protected void test_RowDataBound(object sender, GridViewRowEventArgs e)
{
     DropDownList ddlSelection = new DropDownList();


     string value  = "";
     if (e.Row.RowType == DataControlRowType.DataRow)
     {
         DataRow row = ((DataRowView)e.Row.DataItem).Row;
         value = row.Field<String>("[First Row]");
     }
     List<string> mylist = new List<string>();
     mylist.Add("NORMAL");
     mylist.Add("LOW");

     ddlSelection.DataSource = mylist;
     ddlSelection.DataBind();
     ddlSelection.SelectedValue = value;
     e.Row.Cells[0].Controls.Add(ddlSelection);
}

然后我将它导出到excel。一切正常,期望DropDownList需要放在单元格内(参见下图)。

enter image description here

DropDownList超出其单元格。我该怎么办呢?

1 个答案:

答案 0 :(得分:0)

这是部分解决方案

e.Row.Cells[0].Width = Unit.Pixel(200);

但我想动态地制作它。是否可以知道DropDownList的宽度?

我尝试了这个,但它不起作用。 ddlSelection.Width值= 0。

e.Row.Cells[0].Width = ddlSelection.Width;