|
Hello there.
I have created a custom control derived from TextBox and want to bind a Property to a BindingSource on my Form. The Problem is if the Current of the BindingSource changes (by changing the current row of the grid - without changing data in the TextBox), the ListChanged Event on my underlying DataSource is also raised (setting my DataSource to be modified). This does only happen when I bind my created Property (even though it's just bridging the original Text). When I bind the Text property of the TextBox the event is not raised.
What am I missing here?
Example:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms;
namespace BindingSourceTest { public partial class Form1 : Form { DataTable dt = null; BindingSource s = null; public Form1() { InitializeComponent();
//Add some data to our datasource dt = new DataTable("table"); dt.Columns.Add("text"); dt.Columns.Add("value");
for (int i = 0; i < 10; i++) dt.Rows.Add("Text " + i.ToString(), i.ToString());
dt.AcceptChanges(); dt.RowChanged += new DataRowChangeEventHandler(dt_RowChanged);
//Create a bindingsource for our datasource s = new BindingSource(dt, ""); dataGridView1.DataSource = s;
//if Text is bound the ListChanged is not raised myTextBox1.DataBindings.Add("MyText", s, "value"); }
void dt_RowChanged(object sender, DataRowChangeEventArgs e) { MessageBox.Show("Changes detected"); }
}
partial class Form1 { /// <summary> /// Required designer variable. /// </summary> private System.ComponentModel.IContainer components = null;
/// <summary> /// Clean up any resources being used. /// </summary> /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param> protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); }
#region Windows Form Designer generated code
/// <summary> /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// </summary> private void InitializeComponent() { this.dataGridView1 = new System.Windows.Forms.DataGridView(); this.Column1 = new System.Windows.Forms.DataGridViewTextBoxColumn(); this.Column2 = new System.Windows.Forms.DataGridViewTextBoxColumn(); this.myTextBox1 = new BindingSourceTest.MyTextBox(); ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit(); this.SuspendLayout(); // // dataGridView1 // this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize; this.dataGridView1.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] { this.Column1, this.Column2}); this.dataGridView1.Location = new System.Drawing.Point(12, 12); this.dataGridView1.Name = "dataGridView1"; this.dataGridView1.RowTemplate.Height = 24; this.dataGridView1.Size = new System.Drawing.Size(255, 308); this.dataGridView1.TabIndex = 0; // // Column1 // this.Column1.DataPropertyName = "text"; this.Column1.HeaderText = "Column1"; this.Column1.Name = "Column1"; // // Column2 // this.Column2.DataPropertyName = "value"; this.Column2.HeaderText = "Column2"; this.Column2.Name = "Column2"; // // myTextBox1 // this.myTextBox1.Location = new System.Drawing.Point(27, 337); this.myTextBox1.Name = "myTextBox1"; this.myTextBox1.Size = new System.Drawing.Size(240, 22); this.myTextBox1.TabIndex = 1; // // Form1 // this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 16F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(284, 413); this.Controls.Add(this.myTextBox1); this.Controls.Add(this.dataGridView1); this.Name = "Form1"; this.Text = "Form1"; ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit(); this.ResumeLayout(false); this.PerformLayout();
}
#endregion
private System.Windows.Forms.DataGridView dataGridView1; private System.Windows.Forms.DataGridViewTextBoxColumn Column1; private System.Windows.Forms.DataGridViewTextBoxColumn Column2; private MyTextBox myTextBox1;
}
public class MyTextBox : System.Windows.Forms.TextBox { [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)] public string MyText { get { return base.Text; } set { base.Text = value; } }
} }
|