2012-09-19 4 views
0

asp.net 프로젝트에서 작업 중입니다. gridview 가지고 rowdatabound 행의 모든 ​​셀에 드롭 다운 목록을 넣을 싶습니다. 그래서 나는 다음과 같은 방법을 가지고있다.드롭 다운 목록이있는 Gridview 행

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) 
{ 
    DropDownList ddl = new DropDownList(); 
    ddl.DataSource = getImpacts(); 
    ddl.DataBind(); 
    if (e.Row.RowType != DataControlRowType.Header) 
    { 

     for (int i = 0; i < e.Row.Cells.Count; i++) 
     { 
      e.Row.Cells[i].Controls.Add(ddl); 

     } 
    } 
} 

문제는 dropdouwnlist가 마지막 셀에만 추가된다는 것입니다. 그리고 내가 디버깅 할 때 for 루프는 모든 셀에서 통과한다. 이것이 어떻게 가능한지 ? 각 열에 대해 드롭 다운 목록의 인스턴스를 만들 수

+0

당신은 동적으로 생성 다시 작성해야합니다 컨트롤을 반복 할 수 다시 게시. 'RowDataBound'는 GridView를'DataBind' 할 때만 발생하기 때문에 대신'RowCreated'를 사용해야합니다. 그러나 당신은'RowDataBound'에서'DropDownList'를 데이터 바인딩해야합니다 : –

답변

1

필요

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) 
{ 
    if (e.Row.RowType != DataControlRowType.Header) 
    { 
     for (int i = 0; i < e.Row.Cells.Count; i++) 
     { 
      DropDownList ddl = new DropDownList(); 
      ddl.DataSource = getImpacts(); 
      ddl.DataBind(); 
      e.Row.Cells[i].Controls.Add(ddl); 
     } 
    } 
} 
+0

'Row_DataBound'의 데이터 바인딩 컨트롤 인'Row_Created'에서 컨트롤을 생성하십시오. 다시 그리드가 다시 데이터 바인딩되지 않을 때 다시 게시 할 때 문제가 발생합니다. –

1

당신이 당신의 루프에 삽입하고 모든 각 셀에 대해

 for (int i = 0; i < e.Row.Cells.Count; i++) 
     { 
      DropDownList ddl = new DropDownList(); 
      ddl.DataSource = getImpacts(); 
      ddl.DataBind(); 

      e.Row.Cells[i].Controls.Add(ddl); 
     } 
관련 문제