如何更改以前选择的Dropdownlist的索引?

时间:2012-07-31 07:58:59

标签: c# gridview indexing

我有每行中下拉列表的模板字段的gridview。当我从下拉列表中选择任何值时,它将改变下面第3行的颜色而不是它。但是当我点击另一个下拉列表时,它正在改变下面第3行的颜色。它,但前一个下拉列表的索引不会更改为0,即“选择”。仍然显示在最后一个下拉列表中选择的值。我只想在我点击一个下拉列表时,所有其他下拉列表应该被索引为0即“选择” “除了我点击过的一个。 我在下拉列表的Selectedindexchange事件中执行以下操作:

protected void GridView1_SelectedIndexChanged(object sender,EventArgs e)
    {

        foreach (GridViewRow gvRow in GridView1.Rows)
        {
               int g1 = gvRow.RowIndex;
               GridView1.Rows[g1].BackColor = Color.White;
         }
            DropDownList ddl = (DropDownList)sender;
            GridViewRow row = (GridViewRow)ddl.NamingContainer;
            int g = row.RowIndex + 3;
            int current_row_index = row.RowIndex;
            int pp = GridView1.Rows.Count;
             GridView1.Rows[g].BackColor = Color.Red;
         }
     }

提前致谢。

2 个答案:

答案 0 :(得分:0)

只需使用 DropdownName.ClearSelection()作为您调用Selectedindexchange事件的其他每个下拉列表。

答案 1 :(得分:0)

也许是这样的:

     protected void GridView1_SelectedIndexChanged(object sender,EventArgs e)
    {
        DropDownList ddl = (DropDownList)sender;
        GridViewRow row = (GridViewRow)ddl.NamingContainer;
        int g = row.RowIndex + 3;
        int current_row_index = row.RowIndex;

        foreach (GridViewRow gvRow in GridView1.Rows)
        {
            gvRow.BackColor = Color.White;

            if (gvRow.FindControl("nameOfTheDropdown") != null && gvRow.RowIndex != current_row_index)
            {
                ((DropDownList)gvRow.FindControl("nameOfTheDropdown")).SelectedIndex = 0;
            }
        }

        GridView1.Rows[g].BackColor = Color.Red;  
     }

此致