public partial class MyForm : Form
{
public string Name { get; set; }
public MyForm()
{
InitializeComponent();
Name = "kobe";
this.textBox1.DataBindings.Add("Text", this, "Name", false, DataSourceUpdateMode.OnPropertyChanged);
}
private void button1_Click(object sender, EventArgs e)
{
Name = "Jordan";
}
}
}
为什么修改textbox1中的值。。num就跟着修改了。我在click中设置Name的值。确不能更新到textbox1中!!!!!!!!????
你设置了OnPropertyChanged时更新,当你的textBox1.Text改变时,相应的值会更新到数据源,这是动态改变数据源的模式,也就是你设置的DataSourceUpdateModel,而如果想要让改变后的数据源再更新到textbox1上来,而需要重新读取数据源,用 this.textBox1.DataBindings["Text"].ReadValue();
那你怎样解释下面的情况::??
为什么我修改了textbox1中值后。。在datagrid中点击了该行后能得到textbox1中更新的值??
DataTable dt = new DataTable();
DataColumn dc ;
dc = new DataColumn();
dc.ColumnName = "Name";
dc.DataType = System.Type.GetType("System.String");
dt.Columns.Add(dc);
dc = new DataColumn();
dc.ColumnName = "Age";
dc.DataType = System.Type.GetType("System.Int32");
dc.Unique = true;
dt.Columns.Add(dc);
DataRow drTemp;
drTemp = dt.NewRow();
drTemp[0] = "Jordan";
drTemp[1] = 10;
dt.Rows.Add(drTemp);
drTemp = dt.NewRow();
drTemp[0] = "KOBE";
drTemp[1] = 20;
dt.Rows.Add(drTemp);
dt.DefaultView.AddNew();
dt.AcceptChanges();//关键
mBindingSource = new BindingSource();
mBindingSource.DataSource = dt;
mBindingSource.Sort = "Age ASC";
this.dataGridView1.DataSource = mBindingSource;
this.textBox1.DataBindings.Add("Text", mBindingSource, "Name", false, DataSourceUpdateMode.OnPropertyChanged);
this.textBox2.DataBindings.Add("Text", mBindingSource, "Age", false, DataSourceUpdateMode.OnPropertyChanged);