在Gridview项目模板字段中设置所有值相同(下拉列表)

时间:2013-08-15 21:02:33

标签: c# asp.net gridview

好的,所以我知道标题不是很好,但我会尝试解释。

我有一个gridview,其下拉列表作为模板字段。我想拥有它,以便如果有人在下拉列表中选择了某些内容,则gridview中的所有下拉列表都会更改为该值。这就是我的想法,但看到评论:

protected void dcDdl_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (recPartDdl.SelectedItem.Text != "All")
        {
           string value = //Need to get the value of the changed drop down.

            foreach(GridViewRow row in allRmaGv.Rows)
            {
                DropDownList dcDropDown = (DropDownList)row.FindControl("dcDdl");
                dcDropDown.SelectedValue = value;

            }
        }
    }

我需要在网格视图中为类似过滤器的excel执行此操作。

2 个答案:

答案 0 :(得分:0)

在循环遍历GridView中的行时,您需要过滤掉行类型,这样您就不会尝试在标题行和/或页脚行中查找控件,因此请在foreeach循环中尝试此操作:

foreach(GridViewRow row in allRmaGv.Rows)
{
    // Try to find the `dcDdl` in data rows only, because they do not exist in header or footer rows
    if (row.RowType == DataControlRowType.DataRow)
    {
        DropDownList dcDropDown = (DropDownList)row.FindControl("dcDdl");
        dcDropDown.SelectedValue = value;
    }
}

更新:

要获取所选下拉列表的值,请执行以下操作:

protected void dcDdl_SelectedIndexChanged(object sender, EventArgs e)
{
    if (recPartDdl.SelectedItem.Text != "All")
    {
        string value = (sender as DropDownList).SelectedValue.ToString();

        foreach(...)
        {
            ...
        }
    }
}

答案 1 :(得分:0)

试试这个:

    protected void dcDdl_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (recPartDdl.SelectedItem.Text != "All")
        {
            DropDownList ddl = (DropDownList)sender;

            //First check type of ddl.SelectedValue and then process
            string value = ddl.SelectedValue;

            foreach(GridViewRow row in allRmaGv.Rows)
            {
                DropDownList dcDropDown = (DropDownList)row.FindControl("dcDdl");
                dcDropDown.SelectedValue = value;
            }
        }
    }