[英]Why property is not referenced when binded value changes
I realized that Property get/set is not referenced when value changes in DataGridView (DataTable is binded to DataGridView) 我意识到当DataGridView中的值更改(DataTable绑定到DataGridView)时,没有引用属性get / set。
My Code is like this 我的代码是这样的
private DataTable _dt;
public DataTable Dt
{
get
{
return _dt;
}
set
{
_dt = value;
}
}
public Form1()
{
InitializeComponent();
Dt = new DataTable();
Dt.Columns.Add("Age");
Dt.Columns.Add("Name");
Dt.Rows.Add(19, "kim");
Dt.Rows.Add(25, "Lee");
dataGridView1.DataSource = Dt;
}
As i unsderstand, binding means that if UI control value changes, original value also changes. 如我所知,绑定意味着如果UI控件值更改,则原始值也会更改。 So I expected that DataTable's Set Property is referenced when i change value on DataGridView. 因此,我希望在更改DataGridView的值时引用DataTable的Set属性。 but didn't. 但是没有。
Please someone explain why is so. 请有人解释为什么会这样。
What you describe is true of MVVM when you are using WPF usually, but is common in WinForms too. 通常,当您使用WPF时,您所描述的内容对MVVM来说是正确的,但在WinForms中也很常见。 You are setting the data source for a whole datagrid though. 但是,您正在为整个数据网格设置数据源。 When you change a value inside the grid this will not trigger set Dt to be called. 当您在网格内更改值时,这不会触发集Dt被调用。 If you want to be notified when a cell value changes you would need to use one of the events of the grid. 如果要在单元格值更改时收到通知,则需要使用网格事件之一。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.