Re: Databinding to user control
- From: "Dave Sexton" <dave@jwa[remove.this]online.com>
- Date: Sun, 25 Jun 2006 17:45:35 -0400
Hi John,
What do you mean, "The data set always reads the data back and considers it
change event when it's not."?
The DataGridView handles the state of row changes correctly in my
experience. DataRows being displayed by a DataGridView are always in an
Unchanged, Modified, Deleted or Added state. While editing a row in the
DataGridView the state of the underlying DataRow remains Unchanged until the
changes are committed by the user, in which case the DataRow becomes
Modified.
Could you possibly have forgotten to call DataSet.AcceptChanges() after you
filled the data? (Calling AcceptChanges() isn't necessary when using a
DataAdapter with the default settings)
BTW, the locks in your get and set accessors for the MsgData property aren't
serving any purpose. Locking the "return" statement is of no use and the
variable assignment and method call should probably not be locked and just
allow race conditions in an asynchronous environment. Also, since it's a
UserControl you'll probably have to call Invalidate to update the display
when MsgData is set so synchronicity will be handled implicitly by the
framework on the UI Thread.
HTH
"John J. Hughes II" <no@xxxxxxxxxxx> wrote in message
news:eHxOOIJmGHA.4864@xxxxxxxxxxxxxxxxxxxxxxx
I have a DataGridView displaying data from a DataSet. To the right of
that I have a custom user control which displays one of the data set
fields. The custom user control is bound to the data set object and
displays the data correctly when I move from row to row.
The problem I am having is the data set always reads the data back and
considers it change even when it's not. I am looking for how to change
the following code so the custom user control can set the pencil on the
DataGridView and only cause the data set to be updated if the data is
changed. I don't want to host the control in the DataGridView.
Since I do this with RichTextControls I am assuming its the way I am doing
it and possible.
My guess is I need to enable PropertyChange or Validated but neither work
so obviously I am doing it incorrectly.
//// The data binding in the main form
//// This version uses OnPropertyChanged and I have tried OnValidation.
//
// ctrlMsgEditor1
//
this.ctrlMsgEditor1.Cursor = System.Windows.Forms.Cursors.Default;
this.ctrlMsgEditor1.DataBindings.Add(new
System.Windows.Forms.Binding("MsgData", this.dsMsg1, "Messages.Data",
true, System.Windows.Forms.DataSourceUpdateMode.OnPropertyChanged));
this.ctrlMsgEditor1.Dock = System.Windows.Forms.DockStyle.Right;
this.ctrlMsgEditor1.Location = new System.Drawing.Point(306, 0);
this.ctrlMsgEditor1.MsgData = null;
this.ctrlMsgEditor1.Name = "ctrlMsgEditor1";
this.ctrlMsgEditor1.Size = new System.Drawing.Size(347, 328);
this.ctrlMsgEditor1.TabIndex = 7;
//// The custom user control
[System.ComponentModel.DefaultBindingProperty("MsgData")]
public partial class ctrlMsgEditor : UserControl
{
private byte[] _MsgData = new byte[1];
[System.ComponentModel.Bindable(true), Browsable(true),
Category("Data"), Description("Encoded data to be displayed")]
public byte[] MsgData
{
get
{
lock (this)
{
return _MsgData;
}
}
set
{
lock (this)
{
this._MsgData = value;
this.DisplayData(this._MsgData);
}
}
}
public ctrlMsgEditor()
{
InitializeComponent();
}
private void DisplayData(byte[] msgData)
{
/// Display the data
}
}
Regards,
John
.
- Follow-Ups:
- Re: Databinding to user control
- From: John J. Hughes II
- Re: Databinding to user control
- References:
- Databinding to user control
- From: John J. Hughes II
- Databinding to user control
- Prev by Date: Re: Add Mozilla engine to Windows forms application
- Next by Date: Send small struct from Win32 to C#
- Previous by thread: Databinding to user control
- Next by thread: Re: Databinding to user control
- Index(es):
Relevant Pages
|