2012-09-27 35 views
1

我想在第一次加载aspx页面时创建一个数据表。我已经放置了我的代码以在类文件中创建一个空行的数据表。以下是创建数据表的代码。将datatable绑定到datagrid并在页面上执行数据表代码ASP.NET

public class PaymentDetailsDataTable 
{ 
    public PaymentDetailsDataTable() 
    { 
     DataTable pventries = new DataTable(); 

     DataColumn col1 = new DataColumn("col1"); 
     DataColumn col2 = new DataColumn("col2"); 
     DataColumn col3 = new DataColumn("col3"); 
     DataColumn col4 = new DataColumn("col4"); 
     DataColumn col5 = new DataColumn("col5"); 
     DataColumn col6 = new DataColumn("col6"); 
     DataColumn col7 = new DataColumn("col7"); 
     DataColumn col8 = new DataColumn("col8"); 
     DataColumn col9 = new DataColumn("col9"); 

     col1.DataType = System.Type.GetType("System.Int32"); 
     col2.DataType = System.Type.GetType("System.String"); 
     col3.DataType = System.Type.GetType("System.String"); 
     col4.DataType = System.Type.GetType("System.String"); 
     col5.DataType = System.Type.GetType("System.String"); 
     col6.DataType = System.Type.GetType("System.String"); 
     col7.DataType = System.Type.GetType("System.String"); 
     col8.DataType = System.Type.GetType("System.Double"); 
     col9.DataType = System.Type.GetType("System.String"); 

     pventries.Columns.Add(col1); 
     pventries.Columns.Add(col2); 
     pventries.Columns.Add(col3); 
     pventries.Columns.Add(col4); 
     pventries.Columns.Add(col5); 
     pventries.Columns.Add(col6); 
     pventries.Columns.Add(col7); 
     pventries.Columns.Add(col8); 
     pventries.Columns.Add(col9); 

     pventries.Rows.Add();   

    } 
} 

在隐藏文件我的代码,我实例化我的课,并试图如下如下使用它:

public partial class create_pv : System.Web.UI.Page 
{ 
    String conn = WebConfigurationManager.ConnectionStrings["pvconn"].ToString(); 

    PaymentDetailsDataTable pmd = new PaymentDetailsDataTable(); 




    protected void Page_Load(object sender, EventArgs e) 
    { 


     /**/if(!IsPostBack) 
      pmd.PaymentDetailsDataTable(); 
      allpventries.DataSource = pmd; 
      allpventries.DataBind(); 
     } 
} 

现在,当我尝试访问该方法PaymentDetailsDataTable这样pmd.PaymentDetailsDataTable()我得到的以下错误

'PaymentDetailsDataTable' does not contain a definition for 'PaymentDetailsDataTable' and no extension method 'PaymentDetailsDataTable' accepting a first argument of type 'PaymentDetailsDataTable' could be found (are you missing a using directive or an assembly reference?) 

如果我改变我的方法的返回类型(PaymentDetailsDataTable)作废它的工作原理,但后来我想绑定数据表格转换为网格视图(称为allpventries),并带来编译器错误。

我该如何实现绑定从其代码库位于不同类的数据表中绑定数据网格?稍后我会将新行添加到数据表中。替代方案也可以实现这一点。

非常新的面向对象编程和C#ASP.NET。

回答

1

PaymentDetailsDataTable应该是一个亚类的DataTablePaymentDetailsDataTable类返回DataTable对象的参考限定的方法。

public class PaymentDetailsDataTable : DataTable 
{ 
    public PaymentDetailsDataTable() 
    { 
     Columns.Add("Col1", typeof(int)); 
     Columns.Add("Col2"); 

     Rows.Add(1, "Foo"); 
     Rows.Add(2, "Bar"); 
    } 
} 

,并在Page_Load中处理程序代码,

protected void Page_Load(object sender, EventArgs e) 
{ 
    if(!IsPostBack) 
    { 
     allpventries.DataSource = new PaymentDetailsDataTable(); 
     allpventries.DataBind(); 
    } 
} 
+0

谢谢。我把你的代码片段和Tim Schmelter的代码,现在它的作品 – MaxI

2

你正在创建中是从来没有分配到任何东西的PaymentDetailsDataTable构造本地DataTable。所以你应该从方法中返回DataTable或者从DataTable继承。我认为这是你的初衷:

public class PaymentDetailsDataTable : DataTable 
{ 
    public PaymentDetailsDataTable() 
    { 
     DataColumn col1 = new DataColumn("col1"); 
     DataColumn col2 = new DataColumn("col2"); 
     DataColumn col3 = new DataColumn("col3"); 
     DataColumn col4 = new DataColumn("col4"); 
     DataColumn col5 = new DataColumn("col5"); 
     DataColumn col6 = new DataColumn("col6"); 
     DataColumn col7 = new DataColumn("col7"); 
     DataColumn col8 = new DataColumn("col8"); 
     DataColumn col9 = new DataColumn("col9"); 

     col1.DataType = System.Type.GetType("System.Int32"); 
     col2.DataType = System.Type.GetType("System.String"); 
     col3.DataType = System.Type.GetType("System.String"); 
     col4.DataType = System.Type.GetType("System.String"); 
     col5.DataType = System.Type.GetType("System.String"); 
     col6.DataType = System.Type.GetType("System.String"); 
     col7.DataType = System.Type.GetType("System.String"); 
     col8.DataType = System.Type.GetType("System.Double"); 
     col9.DataType = System.Type.GetType("System.String"); 

     this.Columns.Add(col1); 
     this.Columns.Add(col2); 
     this.Columns.Add(col3); 
     this.Columns.Add(col4); 
     this.Columns.Add(col5); 
     this.Columns.Add(col6); 
     this.Columns.Add(col7); 
     this.Columns.Add(col8); 
     this.Columns.Add(col9); 

     this.Rows.Add(); 
    } 
} 
+0

+1我用这个代码 – MaxI

相关问题