[英]Sorting DataGrid Column as Date
我有一个正在编写的程序可以帮助我的公司跟踪工具校准。 我将所有工具都保存在SQLite数据库中,该数据库没有将列类型设置为DATETIME的选项。 因此,为了简化起见,我将日期存储为M / D / YYYY格式。
我有模型从数据库中提取工具清单,然后将填充的表返回到viewmodel。
从这里开始,我将viewmodel数据表绑定到数据网格,还将每个数据网格列绑定到数据表中的相应列。
我希望用户能够将“到期校准”列从最新到最旧或从最旧到最新进行排序。
问题在于,由于SQLite和DataGrid控件似乎都没有DateTime列的选项,因此datagrid继续将它们作为字符串排序。
DataGrid列设置为DataGridTextColumns,因为我无法确定模板化的列是否可以解决此问题,甚至无法解决该问题。
I.E. :
9/26/2017
9/12/2017
8/5/2017
8/28/2017
我试过将日期转换为MM / DD / YYYY格式,但这没有用。 有人可以帮我弄清楚我需要怎么做才能对这些日期进行正确的排序吗?
我正在使用Caliburn.Micro和SQLite,这有助于缩小可能的解决方案的范围。
CheckOutInModel:
public DataTable RetrieveToolRoster()
{
string db_command = "SELECT [id], [cal_date] FROM inventory WHERE [cal_date] IS NOT NULL ORDER BY [id] ASC;";
SQLiteConnection db_connection = new SQLiteConnection(Properties.Settings.Default.db_connectionstring);
SQLiteDataAdapter db_dataAdapter = new SQLiteDataAdapter(db_command, db_connection);
DataTable tr_dataTable = new DataTable();
try
{
db_connection.Open();
db_dataAdapter.Fill(tr_dataTable);
db_connection.Close();
return tr_dataTable;
}
catch (Exception ex)
{
MessageBox.Show("Error:\r\n" + ex.Message);
return null;
}
}
CheckOutInViewModel:
private DataTable _toolRoster;
public DataTable ToolRoster
{
get { return _toolRoster; }
set
{
_toolRoster = value;
NotifyOfPropertyChange(() => ToolRoster);
}
}
public void PopulateToolRoster()
{
CheckOutInModel coim = new CheckOutInModel();
ToolRoster = coim.RetrieveToolRoster();
}
CheckOutInView:
<DataGrid Grid.Column="0"
ItemsSource="{Binding ToolRoster}"
Style="{DynamicResource DataGridStandard}">
<DataGrid.Columns>
<DataGridTextColumn Header="Tool ID"
Width="*"
Binding="{Binding id}"/>
<DataGridTextColumn Header="Calibration Due"
Width="*"
Binding="{Binding cal_due, StringFormat={}{0:d}}"/>
</DataGrid.Columns>
</DataGrid>
谢谢!
解决方案
我将数据从填充的数据表转移到列表中,然后返回列表。
CheckOutInViewModel:
private List<RosterData> _toolRoster;
public List<RosterData> ToolRoster
{
get { return _toolRoster; }
set
{
_toolRoster = value;
NotifyOfPropertyChange(() => ToolRoster);
}
}
CheckOutInModel:
public List<RosterData> RetrieveToolRoster()
{
string db_command = "SELECT [id], [cal_date] FROM inventory WHERE [cal_date] IS NOT NULL ORDER BY [id] ASC;";
SQLiteConnection db_connection = new SQLiteConnection(Properties.Settings.Default.db_connectionstring);
SQLiteDataAdapter db_dataAdapter = new SQLiteDataAdapter(db_command, db_connection);
DataTable tr_dataTable = new DataTable();
try
{
db_connection.Open();
db_dataAdapter.Fill(tr_dataTable);
db_connection.Close();
List<RosterData> rd = new List<RosterData>();
foreach (DataRow dr in tr_dataTable.Rows)
{
RosterData rds = new RosterData();
rds.id = dr[0].ToString();
rds.cal_date = Convert.ToDateTime(dr[1]);
rd.Add(rds);
}
return rd;
}
catch (Exception ex)
{
MessageBox.Show("Error:\r\n" + ex.Message);
return null;
}
}
RosterData.cs:
public class RosterData
{
public string id { get; set; }
public DateTime cal_date { get; set; }
}
只需定义您的自定义类即可,而不是加载数据表。 使用SQLiteDateReader并将每条记录转换为自定义类的List的元素
public class RosterData
{
public int id {get;set;}
public DateTime cal_date {get;set;}
}
public List<RosterData> RetrieveToolRoster()
{
string List<RosterData> result = new List<RosterData>();
string db_command = "SELECT [id], [cal_date] FROM inventory WHERE [cal_date] IS NOT NULL ORDER BY [id] ASC;";
using(SQLiteConnection db_connection = new SQLiteConnection(Properties.Settings.Default.db_connectionstring))
using(SQLiteCommand cmd = new SQLiteCommand(db_command, db_connection))
{
try
{
db_connection.Open();
using(SQLiteDataReader reader = cmd.ExecuteReader())
{
while(reader.Read())
{
RosterData rd = new RosterData()
{
rd.id = Convert.ToInt32(rd["id"]);
rd.cal_date = Convert.ToDateTime(rd["cal_date"]);
};
result.Add(rd);
}
}
return result;
}
catch (Exception ex)
{
MessageBox.Show("Error:\r\n" + ex.Message);
return null;
}
}
}
.......
private List<RosterData> _toolRoster;
public List<RosterData> ToolRoster
{
get { return _toolRoster; }
set
{
_toolRoster = value;
NotifyOfPropertyChange(() => ToolRoster);
}
}
以数字形式存储/检索可能会更容易。 然后,您可以在查询时对其进行排序,并在显示时简单地转换回来。
DateTime.Ticks
将为您提供很长的时间(纳秒数)。
然后可以将其存储在数据库中,并通过以下方式将其转换为DateTime:
new DateTime(number)
这样,对查询进行排序就很容易了,因为“打勾”次数最多的那个就是将来的DateTime。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.