2012-10-05 4 views
3

BoundFields를 사용하여 GridView를 동적으로 생성했습니다. DataBound 이벤트에서 BoundField 값을 변경하고 싶습니다. 이 값에는 부울 값 (True/False)이 포함되어 있으므로 활성 "/"비활성 "으로 변경해야합니다. 이 GridView 동적 않을 경우 TemplateField, 사용하지만 GridView 동적으로 만드는 오전, 가장 쉬운 방법은 BoundField 할 수 있습니다.DataBound 이벤트에서 GridView BoundField 값을 변경하십시오.

하지만 정확히 어떻게 바꾸는 지 이해할 수 없습니다.

이 제대로 발사되는 내 바인딩 된 이벤트 : BoundFields

protected void gr_RowDataBound(object sender, GridViewRowEventArgs e) 
    { 
     DataRowView drv = (DataRowView)e.Row.DataItem; 
     if (e.Row.RowType == DataControlRowType.DataRow) 
     { 
      if (drv["IsRegistered"] != DBNull.Value) 
      { 
       bool val = Convert.ToBoolean(drv["IsRegistered"]); 
       //???? HOW TO CHANGE PREVIOUS VALUE TO NEW VALUE (val) HERE? 
      } 
     } 
    } 
+0

이것은 나에게 그렇게 쉬운 것처럼 보이지 않습니다. 나는 웹에서 멋지고 쉬운 예제를 찾으려고 노력했지만 그렇게 할 수 없었다. 또한 일부 다른 열에 대해서는 데이터 형식을 지정하는 추가 메서드를 호출해야합니다. – renathy

+0

나는 비슷한 시나리오를 가지고있다. 바운드 필드를 사용하는 많은 Gridviews. 기본적으로 bool 값은 "True"또는 "False"로 렌더링됩니다. 나는 그들이 "Ja"/ "Nein"과 같이 독일어로 번역되기를 바란다. 내 대답을 참조하십시오 ... – Tillito

답변

6

당신이 예를 들어 Text 숙박 시설의 설정하는 TemplateField에서 컨트롤을 찾을 FindControl을 사용할 수 없습니다. 대신 당신은 Cell-Text을 설정

protected void gr_RowDataBound(object sender, GridViewRowEventArgs e) 
{ 
    DataRowView drv = (DataRowView)e.Row.DataItem; 
    if (e.Row.RowType == DataControlRowType.DataRow) 
    { 
     if (drv["IsRegistered"] != DBNull.Value) 
     { 
      bool val = Convert.ToBoolean(drv["IsRegistered"]); 
      // assuming that the field is in the third column 
      e.Row.Cells[2].Text = val ? "Active" : "Inactive"; 
     } 
    } 
} 

그 외에도에서, 당신도 동적 GridViewTemplateFields를 사용할 수 있습니다. 내 경우

How to add TemplateField programmatically

+0

또한 bool 필드가 무엇인지 모르는 경우에도 작동하는 코드를 게시했습니다. – Tillito

0

, 나는 심지어 이름 또는 부울 값을 포함하는 컬럼의 인덱스를 몰랐다. 따라서 첫 번째 단계에서는 셀 값이 "참"또는 "거짓"인지 확인하고, 그렇다면 인덱스를 기억합니다. 이 후, 나는 인덱스를 알고 아무 것도 없다면 아무 것도하지 않는다. 만약 내가 발견했다면 그 값을 재연한다.

이 내 작업 코드 :

// Cache of indexes of bool fields 
private List<int> _boolFieldIndexes; 

private void gvList_RowDataBound(object sender, GridViewRowEventArgs e) 
{ 
    //-- if I checked and there are no bool fields, do not do anything 
    if ((_boolFieldIndexes == null) || _boolFieldIndexes.Any()) 
    { 
     if (e.Row.RowType == DataControlRowType.DataRow) 
     { 
      //-- have I checked the indexes before? 
      if (_boolFieldIndexes == null) 
      { 
       _boolFieldIndexes = new List<int>(); 
       for (int i = 0; i < e.Row.Cells.Count; i++) 
       { 
        if ((e.Row.Cells[i].Text == "True") || (e.Row.Cells[i].Text == "False")) 
        { 
         // remember which column is a bool field 
         _boolFieldIndexes.Add(i); 
        } 
       } 
      } 
      //-- go through the bool columns: 
      foreach (int index in _boolFieldIndexes) 
      { 
       //-- replace its value: 
       e.Row.Cells[index].Text = e.Row.Cells[index].Text 
        .Replace("True", "Ja") 
        .Replace("False", "Nein"); 
      } 
     } 
    } 
} 

좋은 것은이 어떤 gridview에 대한 작동한다. 그냥 이벤트를 첨부하십시오.

관련 문제