【问题标题】:How to get a value from a column in a DataView?如何从 DataView 中的列中获取值?
【发布时间】:2008-12-22 20:53:52
【问题描述】:

我有一个数据视图定义为:

DataView dvPricing = historicalPricing.GetAuctionData().DefaultView;

这是我尝试过的,但它返回的是名称,而不是列中的值:

dvPricing.ToTable().Columns["GrossPerPop"].ToString();

【问题讨论】:

    标签: c# asp.net dataview


    【解决方案1】:

    您需要使用DataRow 来获取值;值存在于数据中,而不是列标题中。在 LINQ 中,有一个扩展方法可能会有所帮助:

    string val = table.Rows[rowIndex].Field<string>("GrossPerPop");
    

    或者没有 LINQ:

    string val = (string)table.Rows[rowIndex]["GrossPerPop"];
    

    (假设数据字符串...如果不是,请使用ToString()

    如果您有 DataView 而不是 DataTable,那么同样适用于 DataRowView

    string val = (string)view[rowIndex]["GrossPerPop"];
    

    【讨论】:

    • 问题是关于数据视图而不是数据行
    【解决方案2】:

    @Marc Gravell .... 你的回答其实就是这个问题的答案。 您可以从数据视图中访问数据,如下所示

    string val = (string)DataView[RowIndex][column index or column name in double quotes] ;
    // or 
    string val = DataView[RowIndex][column index or column name in double quotes].toString(); 
    // (I didn't want to opt for boxing / unboxing) Correct me if I have misunderstood.
    

    【讨论】:

      【解决方案3】:

      适用于 vb.NET 中的任何人:

      Dim dv As DataView = yourDatatable.DefaultView
      dv.RowFilter ="query " 'ex: "parentid = 1 "
      for a in dv
        dim str = a("YourColumName") 'for retrive data
      next
      

      【讨论】:

        【解决方案4】:

        您需要指定要获取其值的行。我可能更倾向于 table.Rows[index]["GrossPerPop"].ToString()

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2018-07-27
          • 1970-01-01
          • 2018-07-01
          • 2017-06-01
          • 1970-01-01
          • 1970-01-01
          • 2014-06-01
          • 2019-12-24
          相关资源
          最近更新 更多