我所做的如下
dtClaim.DefaultView.Sort = "TransactionDate asc";
dtClaim = dtClaim.DefaultView.ToTable();
ViewState["dtClaim"] = dtClaim;
以下是应用排序之前数据表数据的屏幕截图
但是在使用上面的代码后,我无法使用“TransactionDate”
对数据进行排序修改
这里我展示了我如何准备这个数据表
DataTable dtClaim = new DataTable();
dtClaim = (DataTable)ViewState["dtClaim"];
DataRow drClaim = dtClaim.NewRow();
drClaim["ClaimId"] = count + 1;
drClaim["TextileClaimApplicationId"] = ViewState["TextileClaimApplicationId"].ToString();
drClaim["BankId"] = ViewState["TextileClaimApplicationId"].ToString();
drClaim["BankName"] = ViewState["TextileClaimApplicationId"].ToString();
drClaim["SectorId"] = ViewState["TextileClaimApplicationId"].ToString();
drClaim["IsDisbursment"] = rblIsDisbursment.SelectedValue;
drClaim["TransactionDate"] = txtDisbursementDate.Text == "" ? Convert.ToDateTime(txtRepaymentDate.Text, System.Globalization.CultureInfo.GetCultureInfo("en-GB").DateTimeFormat) : Convert.ToDateTime(txtDisbursementDate.Text, System.Globalization.CultureInfo.GetCultureInfo("en-GB").DateTimeFormat);
drClaim["DisbursementAmount"] = txtDisbursementAmount.Text == "" ? Convert.ToDecimal("0") : Convert.ToDecimal(txtDisbursementAmount.Text);
drClaim["RepaymentPrincipleAmount"] = txtRepaymentPrincipleAmount.Text == "" ? Convert.ToDecimal("0") : Convert.ToDecimal(txtRepaymentPrincipleAmount.Text);
drClaim["RepaymentOtherAmount"] = txtRepaymentOtherAmount.Text == "" ? Convert.ToDecimal("0") : Convert.ToDecimal(txtRepaymentOtherAmount.Text);
dtClaim.Rows.Add(drClaim);
dtClaim.DefaultView.Sort = "TransactionDate";
dtClaim = dtClaim.DefaultView.ToTable();
ViewState["dtClaim"] = dtClaim;
答案 0 :(得分:0)
为了让我的代码将DataTable值识别为DateTime,我必须在创建时设置我的行的数据类型。 (我使用这个DataTable作为我的GridView的数据源,并且很难对其进行排序。)
以前我有
DataTable dt = new DataTable();
dt.Columns.Add("ID");
dt.Columns.Add("StartDate");
我将此更改为
dt.Columns.Add("StartDate", typeof(DateTime));
在我设置列类型之前,将我插入该列的类型更改为DateTime不起作用。
https://msdn.microsoft.com/en-us/library/hfx3s9wd(v=vs.110).aspx