Re: Databinding to user control

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



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



.



Relevant Pages

  • Databinding to user control
    ... I have a DataGridView displaying data from a DataSet. ... I have a custom user control which displays one of the data set fields. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: refresh from DataGridView to bound DataSet
    ... on the data set, some of the fields are null, even though they were ... The datagridview has not data part, it shows the data from the ... bound DataSet so that it stays in sync with the DataGridView. ...
    (microsoft.public.dotnet.framework.adonet)
  • Re: datagridview saving
    ... You can bind a DataGridView to ... individual rows in the data grid view), then the data set is just fine. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Databinding to user control
    ... control displays one of the columns correctly via the data binding using the ... MsgData set property. ... The DataGridView handles the state of row changes correctly in my ... that I have a custom user control which displays one of the data set ...
    (microsoft.public.dotnet.languages.csharp)