【问题标题】:Alter values in a column using a foreach before databind gridview在数据绑定 gridview 之前使用 foreach 更改列中的值
【发布时间】:2011-09-13 19:18:22
【问题描述】:

我已经写了这么多......但是我如何将 encryptedstatusid 分配给 listDTO 以进行数据绑定?

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,然后只有它会显示在网格中

【问题讨论】:

  • 这段代码有什么问题? EncryptedStatusId 列是否数据绑定在您的 GridView 中?
  • u cud 保留一个计数器并执行 listDTO[counter] = statusID,忘记不要增加计数器 ;)

标签: c# asp.net gridview datasource


【解决方案1】:

我认为您应该使用另一个列表来执行此操作,因为现有列表在迭代时无法更新。参考以下代码:

  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();

希望这会有所帮助..

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多