1

I'm working with winforms and hit a problem.

I have some data in sql and one of the columns is a sql generated unix timestamp. The sql is updating itself, so I can´t change the data in the datatable.

So i'm populating the datatable and then i use this ("to get the newest items"):
DataView dv = db.historyDataSet.Tables["historyTable"].AsEnumerable().Where(r => (Convert.ToInt64(r["timestamp"])) > (time)).AsDataView();

This generates my dataview, and from that I want to simply populate a dataGridWiew by: dataGridView.DataSource = dv; But how do I get the datagridview to show human readable time (I am using a function for that called timeConvert - the conversion is not my problem)

I do this:

foreach (DataGridViewRow row in batchvisGridView.Rows)
{
DataRow dr = ((DataRowView)row.DataBoundItem).Row;
dr["timestamp"] = timeConvert(Convert.ToInt64(dr["timestamp"]));
}

But I get an error that says I cant store the string in the cell because it has a different type: Additional information: Inputstring was not in correct format.<10:35 26/03-16> couldn' t be saved in timestamp. Expected type is Int32.

How do I do this the best way? (and I don' t want to change database)

Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
  • I hope this can help you http://stackoverflow.com/questions/5524075/programatically-add-new-column-to-datagridview – ganchito55 Mar 26 '16 at 10:06
  • I suggest making a backup of the original data table and use a different dataview for displaying in the user interface so you're only concern would be changing the datetime format of the timestamp column values – John Ephraim Tugado Mar 26 '16 at 10:09
  • What's the data type for `timestamp` column in database table? What's the type of the column in your datatable? – Reza Aghaei Mar 26 '16 at 11:16
  • Timestamp is Int64 in dataTable and sql. But when converted to human readable its a string. – Michael Guldbrand Mar 26 '16 at 13:21
  • You don't need to add any other column to the `DataTable`. You can use [`CellFormatting`](https://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.cellformatting(v=vs.110).aspx) event of grid to format `Int64` value and show it as `DateTime` – Reza Aghaei Mar 26 '16 at 14:44

0 Answers0