我将listview绑定到datatable变量。我的想法是在从ListView删除,插入,更新请求时修改DataTable,并仅在用户需要时将其保存到数据库。但是,在回发后,我的DataTable变为null。有什么办法可以解决吗?这是在没有连接数据库的情况下绑定ListView的正确方法吗?
protected void Page_Load(object sender, EventArgs e)
{
if(!Page.IsPostBack){
DataTable DTReceipts = new DataTable("DTReceipts");
DTReceipts.Columns.AddRange(new DataColumn[] {
new DataColumn("ExpenseReceiptID", Type.GetType("System.Int32")),
new DataColumn("TransactionSeqNumber", Type.GetType("System.Int32")),
new DataColumn("TransactionSeqSub", Type.GetType("System.String")),
new DataColumn("TransactionDate", Type.GetType("System.DateTime")),
new DataColumn("ReceiptNumber", Type.GetType("System.String")),
new DataColumn("Vendor_EntityID", Type.GetType("System.Int32")),
new DataColumn("Description", Type.GetType("System.String")),
new DataColumn("SubProjectID", Type.GetType("System.Int32")),
new DataColumn("AllocationID", Type.GetType("System.Int32")),
new DataColumn("AllocationScope", Type.GetType("System.String")),
new DataColumn("DeductFromVendor_EntityID", Type.GetType("System.Int32")),
new DataColumn("DeductionAmount", Type.GetType("System.Double")),
new DataColumn("EmployeeID", Type.GetType("System.Int32"))
});
for (int i = 1; i <= 20; i++)
{
DataRow drr = DTReceipts.NewRow();
drr["ExpenseReceiptID"] = DBNull.Value;
drr["TransactionSeqNumber"] = i.ToString();
drr["TransactionSeqSub"] = "";
drr["TransactionDate"] = DBNull.Value;
drr["ReceiptNumber"] = DBNull.Value;
drr["Vendor_EntityID"] = DBNull.Value;
drr["Description"] = DBNull.Value;
drr["SubProjectID"] = DBNull.Value;
drr["AllocationID"] = DBNull.Value;
drr["AllocationScope"] = DBNull.Value;
drr["DeductFromVendor_EntityID"] = DBNull.Value;
drr["DeductionAmount"] = DBNull.Value;
drr["EmployeeID"] = DBNull.Value;
DTReceipts.Rows.Add(drr);
}
ExpenseReceiptsList.DataSource = DTReceipts;
ExpenseReceiptsList.DataBind();
}
}