How to set DataTable columns value automatically when saving?

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

Mika M

Hi!

My table contains two columns to save ID-number who created (ie.added) the
row into the table ("CreatorId"-column), and who was last modifier to the
row ("ModifierId"-column). These fields are not visible on the form, only in
one of the dataset's datatable, and application should maintain these fields
automatically.

It's easy to maintain "CreatorId"-columns value just to set default value
for this column to user's ID integer value intCurrentUserID like ...

ds.Tables(0).Columns("CreatorId").DefaultValue = intCurrentUserID

.... but I'm wondering how to maintain "ModifierId"-columns value ? It should
always change to 'intCurrentUserID' when this user for this ID number is
editing and saving changes, then only those updated rows in datatable this
user edited should contain intCurrentUserID value in the
"ModifierId"-column. I'm updating this datatable using DataAdapter's
Update-method.
 
create a new column and set its .Expression property to the value you need
it to be. Every time one fo the source columns changes, it will too.
 
Thanks William for your reply!

.... but I didn't fully understood your advise :\ Maybe you give me some more
advise, please? I tried like this way ...

ds.Tables(0).Columns("ModifierID").Expression = intCurrentUserID.ToString()

.... but when tried to save it, just got the following error message ...

"The column mapping from SourceColumn 'ModifierID' failed because the
DataColumn 'ModifierID' is a computed column."

I know "ModifierID" is column in database source table which I retrieve too
into dataset datatable, so that's why .Expression doesn't work, but what did
you mean?
 
Hi Mika:

I wasn't aware of that column being computed, but by computer I'm assuming
you mean computed in the DB or DataTable.Compute?

Here's an example of how to make everything 0 in ModifierID
int intCurrentUserId = 0;

DataColumn dc = new DataColumn("ModifiedID",
System.Type.GetType("System.String"));

dc.Expression = intCurrentUserId.ToString();

ds.Tables[0].Columns.Add(dc);

dataGrid1.DataSource = ds.Tables[0];



However, if that's a computer column, make a different one and set its
expression to whatever the value needs to be.



HTH,

Bill
 
Back
Top