2012-04-08 9 views
0

にUNIX時間の変換します通常の日付として明示的に表示するは、私はこの日付列がUnixの時間に保存されていると、私が欲しいれる</p> <p>下のコードを使用して、日付列が含まれているSQLite3のデータベースから私のプログラムでデータグリッドビューを移入しています通常の日付と時刻のC#

データベースをデータグリッドビューに読み込む方法はありますか?

SQLiteConnectionStringBuilder csb = new SQLiteConnectionStringBuilder(); 
csb.DataSource = Path.Combine(connectionPath, "sms.db"); 

SQLiteConnection connection = new SQLiteConnection(csb.ConnectionString); 
connection.Open(); 

// SQL query to read the data fromt he database 
SQLiteCommand command = connection.CreateCommand(); 
//Read Everything 
string query = "SELECT * FROM message"; 

command.CommandText = query; 

SQLiteDataAdapter dataAdaptor = new SQLiteDataAdapter(command); 
DataSet dataset = new DataSet(); 
dataAdaptor.Fill(dataset, "Messages"); 
// Get the table from the data set 
DataTable datatable = dataset.Tables["Messages"]; 

dataGridSMS.DataSource = datatable; 
+1

http://www.sqlite.org/lang_datefunc.html – MarcinJuraszek

答えて

3
// This is an example of a UNIX timestamp for the date/time 
double timestamp = 1116641532; 

// First make a System.DateTime equivalent to the UNIX Epoch. 
System.DateTime dateTime = new System.DateTime(1970, 1, 1, 0, 0, 0, 0); 

// Add the number of seconds in UNIX timestamp to be converted. 
dateTime = dateTime.AddSeconds(timestamp).ToLocalTime(); 
0

使用DataGridView.CellFormatting Event

private void dataGridSMS_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e) 
    { 
     if (this.dataGridSMS.Columns[e.ColumnIndex].Name == "dateColumn") 
     { 
      if (e.Value != null) 
      { 
       try 
       { 
        // Formatting 
        double timestamp = Convert.ToDouble(e.Value); // if e.Value is string you must parse 
        System.DateTime dateTime = new System.DateTime(1970, 1, 1, 0, 0, 0, 0); 
        dateTime = dateTime.AddSeconds(timestamp).ToLocalTime(); 
        e.Value = dateTime.ToString(); 
        e.FormattingApplied = true; 
       } 
       catch (Exception) 
       { 
        e.FormattingApplied = false; 
       } 
      } 
     } 
    } 
関連する問題