2017-02-21 20 views
0

我需要一個C#中的數據表可以通過其他方法訪問......並且我被告知我需要將一個數據表作爲一個類,而不是像以下示例中的方法那樣創建一個數據表:作爲一個類的C#數據表

//my existing datatable method  
public DataTable Conditions(){ 
     DataTable dtConditions = new DataTable(); 

     DataColumn firstParameterID = new DataColumn("firstParameterID ", typeof(int)); 
     dtConditions.Columns.Add(firstParameterID); 

     DataColumn secondParameterID = new DataColumn("secondParameterID ", typeof(int)); 
     dtConditions.Columns.Add(secondParameterID); 
     /* 
     * more columns and rows... 
     */ 
     return dtConditions; 
    } 

我的問題是:我怎麼把它或者我可以把它放在一個名爲dataTableConditions.cs一個單獨的類文件....

我創建了一個類文件,並在這裏是我,但接下來呢?

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Data; 

namespace myHomeworkProject 
{ 
    class dataTableConditions 
    { 
     //How do I make this class and later how can I access its rows, I mean, how can I assign values to these rows later from other methods? 
    } 
} 

謝謝!

+0

你的意思是作爲一個類屬性? – Vijay

+1

我想你被告知有另一個存儲你的'DataTable'方法的類。我第一次聽說'DataTable是一個類'? – Badiparmagi

+0

原諒我的無知,因爲我是初學者:)謝謝 – Volkan

回答

1

既然你創建以下

DataTable dtConditions = new DataTable(); 

您shoulk創建DataTable類。類可以看起來像:

using System.Collections.Generic; 
 

 
namespace someNamespace { 
 

 
    public class DataTable { 
 

 
     public List<DataColumn> Columns {get;set;} 
 
    } 
 

 
}

ColumnsList對象存儲所述不同DataColumns。您還可以創建存儲不同行的List屬性。

.Net框架還包含數據表類的默認實現。這link顯示了這個類的「API」是怎麼樣的。這個link描述瞭如何編寫你自己的數據表類。

希望有所幫助。

相關問題