我想在第一次加载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。
谢谢。我把你的代码片段和Tim Schmelter的代码,现在它的作品 – MaxI