Readonly Datatable

  • Thread starter Thread starter Charles Zhang
  • Start date Start date
C

Charles Zhang

I have a "static" datatable in a class and the datatable is loaded by
the type constructor. I want to make the datatable as read only, is it
possible without using events to rollback the changes?

Thanks

Charles Zhang
 
By readonly, I take it you mean that no one should be able to modify the
contents of the datatable?

If so, the only way to do that, would be to make it private, and write
methods that are wrappers for the functionality you want to expose. So you
may want to write some Get methods, to get data out of the datatable. But
you wouldn't have any to set the data.
 
Marina,

You confuse me with your answer.

In my idea

Making a database table read only is just giving no rights to the user in
the database to change.
Using a DataTable read only is just disable all controls that are binded to
it and not creating procedures around it that use other controls.

And if you don't want to update the database with the datatable. Don't
create an update method.

What do I see wrong?

Cor
 
I understood the question as having a variable of type DataTable, where the
developer cannot change any of the values in that datatable. The developer
can only read values from it.

That way my interpretation of the question. The way the question was
phrased, it was ambigous, and I happen to understand it this way.

If the question was how to not allow the user to make changes to a table,
then of course the answer is very different.
 
That is an interesting idea. My first feeling was: it would not work
since "AllowEdit" only apply to the view not the data table itself.

To make sure, I tried something like:
myDataTable.DefaultView.AllowEdit = false;
myDataTable[0][0] ="Try to Change";
myDataTable.AcceptChanges();

Then I used the debugger to see the value of myDataTable[0][0]
The content of the field was change to "Try to Change"

Thanks

Charles Zhang
 
Marina,

Now I understand what you mean, and I did not see that in advance, however.

From you: the one who is one of the regulars who is the most against
Strongly Typed Datasets in this newsgroup (you are not alone), while this is
in my idea a typical Strongly Typed DataSet answer,

:-)

Cor
 
Hi Charles,
According to your description,
I understand that you want to make the DataTable as read only,
If I misunderstood anything here, please don't hesitate to correct me.

"myDataTable.DefaultView.AllowEdit = false" means that DataGrid or GridView
binding to this DataTable can not Edit.
But you still can edit the rows in DataTable by code.(e.g:
myDataTable[0][0] ="Try to Change";)

So we suggest you can use the method ("DataTable.RejectChanges()") when
there is an event("DataTable.RowChanged") fired.
Such as:
myDataTable.RowChanged += new DataRowChangeEventHandler(dt_RowChanged);
void dt_RowChanged(object sender, DataRowChangeEventArgs e)
{
myDataTable.RejectChanges();
}

But the event "dt_RowChanged" will be fired twice.
The fist time is due to the statement "myDataTable.Rows[0][0] ='Try to
Change'".
And the second time is due to the method "DataTable.RejectChanges()".

If there is any question, please feel free to reply here and I am glad to
work with you.
Wen Yuan
===============================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
===============================
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
Thanks.

I was using as DataTable just like an memory array. Since it is
difficult to make it read only, I have decided to store the info as real
array.

Charles Zhang
 
Back
Top