Re: Databinding to user control
- From: "Dave Sexton" <dave@jwa[remove.this]online.com>
- Date: Mon, 26 Jun 2006 14:18:15 -0400
Hi John,
I tried to piece together your problem from snippets of your last post. I
have responded to each snippet inline, below. If you could possibly
condense your question into one line, if I haven't answered it in this post,
it would be much easier to answer.
When the row is changed the new row is also displayed correctly. The
problem is every time the row is changed the MsgData is read using get and
the DataSet considers it changed.
Sounds correct to me. If you change a field value in a DataRow the
DataRow.RowState property will become "Modified". The DataSet does not
consider a DataRow changed just by reading a value.
So if I do a dataset.GetChanges() every row viewed reports changes made
when no changes were made.
If you want to commit all changes to a DataSet so that GetChanges() returns
null simply call DataSet.AcceptChanges(). Editing the DataRow in the
DataGridView will cause GetChanges() to return changed DataRows until
DataSet.AcceptChanges() is called. If DataSet.GetChanges() returns null
before the data is bound to a DataGridView it will remain null until a
DataRow is edited and the changes are committed. DataSet.AcceptChanges()
should be called immediately after the DataSet is filled with data, unless
your using a DataAdapter or table adapter will the default settings.
In fact currently the control does not allow changes and whatever is set
is return
Do you mean to say that your control does not provide a way for users to
edit the bound data? The DataSet will not mark a DataRow as "Modified"
because it is bound to a control, it will only mark a DataRow as "Modified"
if the value is changed. Reading a value does not qualify as changing a
value. If you don't provide code in your control that changes the value of
a column in a bound DataRow then the DataRow.RowState will remain
"Unchanged" until the data is edited and committed or the row is deleted.
I assume there is some method which I have not found to cause the DataSet
to be notified that the data in the control is being edit
DataRow.BeginEdit()
DataRow.EndEdit()
DataRow.CancelEdit()
If I do the same thing but use a string and text field the DataSet does
not consider the data changed just by changing rows but does apply the
changes and mark the DataSet as changed when the text in the control is
updated.
Navigating the bound DataSet of a DataGridView will not cause the DataSet to
change the state of one of its DataRows. The value of a DataColumn for a
DataRow must be edited. I suspect that if you are simply navigating the
DataSet by selecting a row in the DataGridView and then detecting changes
using the GetChanges() method that the changes already existed before you
selected the row or you have code executing in an event handler that makes
changes to the underlying DataRow when navigating the data source.
Mind you the problem might lay in the DataSet being unable to determine if
the data has changed being its a byte array. If so do I need to supply a
special object to deal with the comparison.
No. The DataSet will correctly detect changes to a DataColumn of a DataRow
simply by setting the value. The DataSet is not concerned with the Type or
content of the value.
Here's a simple example of using a DataTable as a data source for a
DataGridView. I intend to show when and why the DataTable.GetChanges()
method will return a non-null value. The sample will work with a DataSet
just as well.
Please realize that I have not attempted to build this code.
private DataTable table;
private void InitializeDataAndBind()
{
table = new DataTable();
table.Columns.Add(typeof(string), "Column1");
table.Rows.Add("Row 1");
table.Rows.Add("Row 2");
// commit changes to DataTable
table.AcceptChanges();
DataTable changes = table.GetChanges();
// changes is null
// bind the DataTable to a DataGridView
dataGridView1.DataSource = table;
}
/*
After the data is initialized and bound to the grid select a row in the
grid.
Then execute code to call table.GetChanges() by clicking a button on the
Form.
table.GetChanges will return null unless a DataRow was actually modified.
Using the DataBindings of a custom control will not change this behavior.
If your control modifies a DataRow
then GetChanges() will return a DataTable containing a clone of the modified
DataRow until
table.AcceptChanges() is called. If your control does not modified the
DataRow to which it's bound then
table.GetChanges() will always return null until a DataRow is modified,
deleted or added.
*/
private void TestEdit()
{
// I assume table.GetChanges() returns null at this point for the
purpose of this demonstration
// Call this method before making changes to the DataTable.
// edit a row in code...
DataRow rowInEdit = table.Rows[0];
// call rowInEdit.BeginEdit() if you intend to keep the row in edit mode
until a later time
// in that case changes will not be commited to the row until
rowInEdit.EndEdit() is called.
// edit a row
rowInEdit["Column1"] = "Row 1 (edited)";
// now, table.GetChanges() will return a DataTable containing a clone of
rowInEdit since it was modified
// message will be displayed since the row was modified above
System.Diagnostics.Debug.WriteLineIf(rowInEdit.RowState ==
DataRowState.Modified,
"DataRow is Modified");
// accept all changes to the table
table.AcceptChanges();
DataTable changes = table.GetChanges();
// changes is null
// message will be displayed since all changes have been accepted.
System.Diagnostics.Debug.WriteLineIf(rowInEdit.RowState ==
DataRowState.Unchanged,
"Changes have been accepted. The DataRow is no longer
'Modified'.");
}
HTH
.
- References:
- Databinding to user control
- From: John J. Hughes II
- Re: Databinding to user control
- From: Dave Sexton
- Re: Databinding to user control
- From: John J. Hughes II
- Databinding to user control
- Prev by Date: Re: Control.Invoke and ref parameter
- Next by Date: Sockets class question - sending TCP data
- Previous by thread: Re: Databinding to user control
- Next by thread: Re: Databinding to user control
- Index(es):