Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/37.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用数组元素填充gridview中的DropDownList_C#_Asp.net_Arrays_Gridview - Fatal编程技术网

C# 使用数组元素填充gridview中的DropDownList

C# 使用数组元素填充gridview中的DropDownList,c#,asp.net,arrays,gridview,C#,Asp.net,Arrays,Gridview,我一直在尝试用数组元素填充gridview中的DropDownList。数组由另一个gridview中的列名组成。array元素似乎从其源获取列名,但我不知道如何将其提供给dropdownlist。以下是我的代码-: public partial class Default2 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { string[] exce

我一直在尝试用数组元素填充gridview中的DropDownList。数组由另一个gridview中的列名组成。array元素似乎从其源获取列名,但我不知道如何将其提供给dropdownlist。以下是我的代码-:

   public partial class Default2 : System.Web.UI.Page
    {
     protected void Page_Load(object sender, EventArgs e)
    {
    string[] excel = new string[250];
    DataTable dtt = (DataTable)Session["griddata"];  //griddata is the gridview data from another page        
    for (int i = 0; i < dtt.Columns.Count; i++)
    {
        excel[i] = dtt.Columns[i].ColumnName;
    }
    Session["exceldata"] = excel;
    ArrayList mylist= (ArrayList)Session["exceldata"];
    DropDownList drd = (DropDownList)GridView2.FindControl("DrdDatabase");
    drd.DataSource = mylist;
    drd.DataTextField = "GridView";
    drd.DataBind();
    }
public部分类Default2:System.Web.UI.Page
{
受保护的无效页面加载(对象发送方、事件参数e)
{
字符串[]excel=新字符串[250];
DataTable dtt=(DataTable)会话[“griddata”];//griddata是来自另一页的gridview数据
for(int i=0;i

提前感谢:)

您只需循环它并以编程方式添加
列表项即可:

DropDownList drd = (DropDownList)GridView1.FindControl("DrdDatabase");
foreach(string colName in mylist)
    drd.Items.Add(new ListItem( colName ));
但是,您是否确定通过
GridView1.FindControl
找到了您的
DropDownList
?我假设您在那里得到了
NullReferenceException
。然后您需要向我们显示它的实际位置

如果它位于
GridView
TemplateField
中,则应使用
RowDataBound
事件:

private ArrayList ExcelData
{
    get {
        object excel = Session["exceldata"];
        if (excel == null) Session["exceldata"] = new ArrayList();
        return (ArrayList)Session["exceldata"];
    }
    set {
        Session["exceldata"] = value;
    }
}

protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        DropDownList ddl = (DropDownList)e.Row.FindControl("DrdDatabase");
        foreach (string colName in ExcelData)
            ddl.Items.Add(new ListItem(colName));
    }
}

您可以简单地循环它并以编程方式添加
列表项

DropDownList drd = (DropDownList)GridView1.FindControl("DrdDatabase");
foreach(string colName in mylist)
    drd.Items.Add(new ListItem( colName ));
但是,您是否确定通过
GridView1.FindControl
找到了您的
DropDownList
?我假设您在那里得到了
NullReferenceException
。然后您需要向我们显示它的实际位置

如果它位于
GridView
TemplateField
中,则应使用
RowDataBound
事件:

private ArrayList ExcelData
{
    get {
        object excel = Session["exceldata"];
        if (excel == null) Session["exceldata"] = new ArrayList();
        return (ArrayList)Session["exceldata"];
    }
    set {
        Session["exceldata"] = value;
    }
}

protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        DropDownList ddl = (DropDownList)e.Row.FindControl("DrdDatabase");
        foreach (string colName in ExcelData)
            ddl.Items.Add(new ListItem(colName));
    }
}

以下是您可以用来填充dropdownlist的逻辑。 ->

很抱歉没有提供准确的代码,因为我现在没有.net编辑器,但是您可以使用逻辑来实现它


问候。

以下是您可以用来填充dropdownlist的逻辑。 ->

很抱歉没有提供准确的代码,因为我现在没有.net编辑器,但是您可以使用逻辑来实现它


问候。

非常感谢您的帮助,Tim。但是没有rowdatabound事件,因此gridview还无法显示(因为还没有数据)。是否有方法在此gridview中显示dropdownlist。非常感谢!@rawatdeepesh:
RowDataBound
会为每个
GridViewRow
触发,包括
页眉
页脚
数据行
(对每个数据行重复),
EmptyDataRow
Pager
Separator
。所以你要么把下拉列表放在页眉/页脚/页面中,要么放在
EmptyDataTemplate
中。要么根本不把它放在GridView中。太好了,蒂姆·施梅尔特。你节省了我的时间。我很高兴我能帮上忙。如果你觉得答案有用,你可以投赞成票;)非常感谢您的帮助,Tim。但是没有rowdatabound事件,因此gridview还无法显示(因为还没有数据)。是否有方法在此gridview中显示dropdownlist。非常感谢!@rawatdeepesh:
RowDataBound
会为每个
GridViewRow
触发,包括
页眉
页脚
数据行
(对每个数据行重复),
EmptyDataRow
Pager
Separator
。所以你要么把下拉列表放在页眉/页脚/页面中,要么放在
EmptyDataTemplate
中。要么根本不把它放在GridView中。太好了,蒂姆·施梅尔特。你节省了我的时间。我很高兴我能帮上忙。如果你觉得答案有用,你可以投赞成票;)