TimeStamp with DataTable

  • Thread starter Thread starter Mika M
  • Start date Start date
M

Mika M

Hi!

I have DataGrid bound to DataTable. I create DataTable "on fly" when
application start without database connection. I use datagrid only to create
simple XML-file for other purposes.

Question: DataTable contains "Time"-column, and I need to get time into it
automatically of the moment, when user adds a new row into DataGrid, but how
to do it? It can't be only something like dc.DefaultValue = DateTime.Now;
because time is not then time of the moment when DataGrid row was added by
user.

I think this must be easy, but I don't get it yet. Here's some of my code,
if needed...

DataColumn dc;

dc = new DataColumn("Time");
dc.DataType = typeof(DateTime);
dc.DefaultValue = DateTime.Now;
dt.Columns.Add(dc);
....

if(dc.ColumnName.ToString() == "Time") {
textColumn.Format = String.Format("G", textColumn.TextBox);
textColumn.ReadOnly = true;
}
 
Hi there...

You're right about using the default value (".. is not then time of the
moment when DataGrid row was added..."). However you can try handling the
datatable's RowChanged event...

For example:

// Let's suppose table is the datatable you're interested in...
table.RowChanged+=new DataRowChangeEventHandler(table_RowChanged);


// The event handler code look something like this...
private void table_RowChanged(object sender, DataRowChangeEventArgs e) {
if (e.Action.Equals(DataRowAction.Add)) {
e.Row["YourColumnNameHere"] = DateTime.Now;
}

and that's it... hope this might help you...

Regards,
 
Back
Top