簡體   English   中英

將DataGrid列排序為日期

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM