1、datagridview 日期列排序

        private void Form1_Load(object sender, EventArgs e)
{
//方法1
dataGridView1.ColumnHeaderMouseClick += tempView_ColumnHeaderMouseClick;
DataTable dt = Create();
dataGridView1.DataSource = dt; //方法2
dataGridView2.SortCompare += dataGridView2_SortCompare;
fillDatagridview(dataGridView2, dt);
} #region 方法1,已知那一列是日期类型,以datasourcr方式绑定数据
//创建表,为了culture的原因,日期列的数据源类型是字符串而不是日期类型
public DataTable Create()
{
DataTable dt = new DataTable();
try
{
dt.Columns.Add("name");
dt.Columns.Add("birthdate");
dt.Rows.Add("m", "04.01.1969");
dt.Rows.Add("n", "05.12.1961");
dt.Rows.Add("n", "06.04.1963"); }
catch (Exception)
{
throw;
} return dt;
} //方法1,已知那一列是日期类型,以datasourcr方式绑定数据
void tempView_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
{
try
{
if (e.ColumnIndex >= )
{
DataGridView dgv = ((DataGridView)sender);
if (e.ColumnIndex == )
{
//foreach (DataRow row in dt.Rows.Cast<DataRow>().OrderBy(r => DateTime.Parse(r["birthdate"].ToString())))
DataTable dt = (DataTable)dgv.DataSource;
DataTable dtNew = new DataTable();
if (dgv.Tag == null || dgv.Tag.ToString() == "Desc")
{
dgv.Tag = "Asc";
dtNew = dt.Rows.Cast<DataRow>().OrderBy(r => DateTime.Parse(r["birthdate"].ToString())).CopyToDataTable();
}
else
{
dgv.Tag = "Desc";
dtNew = dt.Rows.Cast<DataRow>().OrderByDescending(r => DateTime.Parse(r["birthdate"].ToString())).CopyToDataTable();
}
dgv.DataSource = null;
dgv.DataSource = dtNew;
}
}
}
catch
{ }
}
#endregion #region 方法2 SortCompare,:只适用非datasource方式绑定的数据源,所以用这个方法数据必须一行行添加到gridview
//方法2:只适用非datasource方式绑定的数据源
//并且this.Columns[i].SortMode = DataGridViewColumnSortMode.Programmatic; void dataGridView2_SortCompare(object sender, DataGridViewSortCompareEventArgs e)
{
CompareDate(e);
}
//按照日期比较
private static void CompareDate(DataGridViewSortCompareEventArgs e)
{
try
{
DateTime cell1 = DateTime.MinValue;
DateTime cell2 = DateTime.MinValue;
bool t1 = DateTime.TryParse(e.CellValue1.ToString(), out cell1);
bool t2 = DateTime.TryParse(e.CellValue2.ToString(), out cell2);
if (t1 && t2)
{
e.SortResult = System.DateTime.Compare(cell1, cell2);
e.Handled = true;
}
}
catch (Exception)
{ }
}
//按照数字比较
private static void CompareNumber(DataGridViewSortCompareEventArgs e)
{
try
{
Decimal cell1 = Decimal.MinValue;
Decimal cell2 = Decimal.MinValue;
bool t1 = Decimal.TryParse(e.CellValue1.ToString(), out cell1);
bool t2 = Decimal.TryParse(e.CellValue2.ToString(), out cell2);
if (t1 && t2)
{
e.SortResult = (cell1 == cell2 ? : (cell1 > cell2 ? : -));
e.Handled = true;
}
}
catch (Exception)
{ }
} private void fillDatagridview(DataGridView dataGridView1, DataTable dt)
{
dataGridView1.DataSource = null;
dataGridView1.Rows.Clear();
dataGridView1.Columns.Clear();
for (int i = ; i < dt.Columns.Count; i++)
{
dataGridView1.Columns.Add(dt.Columns[i].ColumnName, dt.Columns[i].ColumnName);
}
for (int i = ; i < dt.Rows.Count; i++)
{
dataGridView1.Rows.Add();
for (int j = ; j < dt.Columns.Count; j++)
{
dataGridView1.Rows[i].Cells[j].Value = dt.Rows[i][j];
}
}
} #endregion #region 其他方法,改变列数据类型
//由于种种原因,dgv上绑定的数据表里面的日期类型是字符串(为了适用各个国家的日期格式,日期类型专门做了格式化),后期再改回正确类型
//在有数据的情况下,列类型是不能修改的,所以用临时列
public static void ChangeColumnDataType(DataGridView dgv)
{
DataTable dt = (DataTable)dgv.DataSource;
if (dt.Rows.Count > )
{
for (int i = ; i < dt.Columns.Count; i++)
{
DateTime time = DateTime.MinValue;
if (DateTime.TryParse(dt.Rows[][i].ToString(), out time))
{
ChangeColumnDataType(dt, dt.Columns[i].ColumnName, typeof(DateTime));
}
} }
}
public static bool ChangeColumnDataType(DataTable table, string columnname, Type newtype)
{
if (table.Columns.Contains(columnname) == false)
return false; DataColumn column = table.Columns[columnname];
if (column.DataType == newtype)
return true; try
{
DataColumn newcolumn = new DataColumn("temporary", newtype);
table.Columns.Add(newcolumn);
foreach (DataRow row in table.Rows)
{
try
{
row["temporary"] = Convert.ChangeType(row[columnname], newtype);
}
catch
{
}
}
table.Columns.Remove(columnname);
newcolumn.ColumnName = columnname;
}
catch (Exception)
{
return false;
} return true;
}
#endregion
05-07 15:29