2012-09-27 2 views
1

aspx 페이지가 처음로드 될 때 데이터 테이블을 만들고 싶습니다. 클래스 파일에 빈 행이있는 데이터 테이블을 만들기 위해 코드를 배치했습니다. 아래는 데이터 테이블을 생성하는 코드입니다. 파일 뒤에 내 코드에서 datagrid에 데이터 테이블 바인딩 (개별 클래스의 코드) 및 페이지로드시 데이터 테이블 코드 실행 C# 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(); 
     } 
} 

을 지금 내가 얻을이 pmd.PaymentDetailsDataTable() 같은 방법 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)에 추가하면 컴파일러 오류가 발생합니다.

코드베이스가 다른 클래스에있는 데이터 테이블에서 DataGrid를 바인딩하는 방법은 무엇입니까? 나중에 데이터 테이블에 새 행을 추가 할 것입니다. 이를 달성하기위한 대체 옵션도 허용됩니다.

객체 지향 프로그래밍 및 C# ASP.NET의 새로운 기능입니다.

답변

1

클래스 PaymentDetailsDataTableDataTable의 하위 클래스이거나 DataTable 오브젝트의 참조를 반환 PaymentDetailsDataTable 클래스의 메소드를 정의한다.

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

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

과를 Page_Load 처리기 코드

, 당신은 아무것도에 할당되지 않습니다 PaymentDetailsDataTable의 생성자에서 로컬 DataTable를 만드는

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

감사합니다. 코드 스 니펫과 Tim Schmelter 코드를 가져 와서 작동합니다. – MaxI

2

. 따라서 메서드에서 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

관련 문제