2011-06-13 90 views
1

我已經寫了這麼多...但是我如何將encryptedstatusid分配給listDTO以用於數據綁定?在數據綁定gridview中使用foreach修改列中的值

protected void Page_Load(object sender, EventArgs e) 
{ 
    List<DTO> listDto; 
    IApplication engine; 

    try 
    { 
     engine = new Engine(); 
     listDto = engine.ReadHistory(Session["UserID"].ToString()); 

     foreach (DTO iterDTO in listDto) 
     { 
     iterDTO.EncryptedStatusId = Triple.Encrypt(iterDTO.StatusId.ToString()); 

     //how i assign the encryptedstatusid to the listDTO for databind????????????? 

     } 

     this.dvHistory.DataSource = listDto; 
     this.dvHistory.DataBind(); 

    } 
    catch (Exception) 
    { 
     throw ;     
    } 
    finally 
    { 
     engine = null; 
    } 
} 

問題是即時得到的encryttedstatusid到iterDTO,但satasource是一個名單,我想encry PTED狀態ID分配給listDto,那麼只有它會在網格顯示

+0

什麼是與此代碼的問題? GridView中的EncryptedStatusId列數據綁定? – 2011-06-13 09:48:20

+0

u cud保留一個計數器並做listDTO [counter] = statusID,忘記不要增加計數器;) – Zenwalker 2011-06-13 09:54:27

回答

0

我想你應該使用另一個列表來做到這一點,因爲迭代時不能更新現有的列表。請參閱下面的代碼:

engine = new Engine(); 
    listDto = engine.ReadHistory(Session["UserID"].ToString()); 

    List<DTO> listDtoNew=new List<DTO>(); 

    foreach (DTO iterDTO in listDto) 
    { 
    iterDTO.EncryptedStatusId = Triple.Encrypt(iterDTO.StatusId.ToString()); 

    listDtoNew.add(iterDTO); 

    } 

    this.dvHistory.DataSource = listDtoNew; 
    this.dvHistory.DataBind(); 

希望這有助於..

相關問題