2012-07-31 146 views
0

我在每行中都有一個帶有dropdownlist模板字段的gridview。當我從下拉列表中選擇任何值時,它會改變比它低的第三行的顏色。但是當我點擊另一個下拉列表時,它正在改變比它低的第三行的顏色但前一個下拉列表的索引不會更改爲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; 
     } 
    } 

感謝。

+0

跟進:如果你有你要找的答案,請把它標記爲正確的。問候 – 2012-08-02 04:37:12

回答

0

只需使用DropdownName.ClearSelection()爲您的每個其他下拉菜單除了您調用Selectedindexchange事件。

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; 
    } 

問候

相關問題