I want to create a data table when the aspx page is first loaded. I have placed my code to create the data table with a blank row in a class file. Below is the code for creating the data table.
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();
}
}
In my code behind file, I have instantiated my class and tried to use it as follows as follows:
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();
}
}
Now when I try to access the method PaymentDetailsDataTable like this pmd.PaymentDetailsDataTable() I get the following error
'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?)
If I change my method return type(PaymentDetailsDataTable) to void it works but I later want to bind the data table to a grid view(called allpventries) and it brings a compiler error.
How may I achieve binding a datagrid from a datatable whose code base is in a different class? Later I will be adding new rows to the datatable. Alternative options also acceptable to achieve this.
Very new in object oriented programming and C# ASP.NET.
The class
PaymentDetailsDataTableshould be a sub-class ofDataTableor define a method inPaymentDetailsDataTableclass which returns reference ofDataTableobject.and code in page_load handler,