大家好:
想詢問C#的datatable sort,有查過網路,我是要排序小數,但是我的欄位很尷尬,並不是每一筆都有值....所以他排序都一直無法由小排到大,請問各位大大有沒有方法,能指點一下嗎?
謝謝
DataTable data = (DataTable)ViewState["data"];
DataTable dtMarks1 = data.Clone();
dtMarks1.Columns[e.SortExpression].DataType = Type.GetType("System.Decimal");
foreach (DataRow dr in data.Rows)
{
dtMarks1.ImportRow(dr);//到這裡會說空白不符合type而錯誤
}
dtMarks1.AcceptChanges();
DataView dv = dtMarks1.DefaultView;
dv.Sort = e.SortExpression + " " + ViewState["dataSortSeq"].ToString();
this.gvResult.DataSource = dv;
this.gvResult.DataBind();
DataTable dtMarks1 = new DataTable();
dtMarks1 = data.Clone();
foreach (DataRow row in data.Rows)
{
DataRow newRow = dtMarks1.NewRow();
for (int i = 0; i < data.Columns.Count; i++)
{
newRow[i] = row.IsNull(i) ? DBNull.Value : row[i];
}
dtMarks1.Rows.Add(newRow);
}
dtMarks1.Columns[e.SortExpression].DataType = typeof(decimal);