【问题标题】:C# - Modifying a Data TableC# - 修改数据表
【发布时间】:2009-01-13 17:20:38
【问题描述】:

我有一个从数据库连接填充的数据表。我想对数据表进行检查,例如用文本替换数字并添加我自己的数据,然后再将其绑定到数据网格以供查看。如何访问数据表中的一条数据?谢谢。

【问题讨论】:

    标签: c# database datagrid datatable


    【解决方案1】:

    只需查看表的 .Rows 集合,并使用普通数组 ([]) 表示法访问每一行中的字段,使用列索引或字段名称作为下标。

    【讨论】:

      【解决方案2】:

      您可以订阅 DataGrid 的 ItemDataBound 事件,然后在 DataGridRow 的内容全部发送回浏览器之前对其进行修改。

      这是an example 的用法。在事件处理程序中,尝试:

      Label lblBalance = (Label)e.Item.FindControl("dgLabel2");
      

      e.Item.Cells[2].Text = "whatever text"
      

      【讨论】:

        【解决方案3】:
        DataSet ds = GetData();
        foreach( DataTable dt in ds.Tables )
        {
           foreach( DataRow row in dt.Rows )
           {
              if ( row["columnName"] != DBNull.Value )
              {
                 row["columnName"] = "some data";
              }
           }
        }
        DataBind();
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2010-11-19
          • 2020-03-22
          • 2010-11-22
          • 1970-01-01
          • 2016-07-19
          • 2015-10-28
          • 2023-03-06
          相关资源
          最近更新 更多