遗忘海岸

江湖程序员 -Feiph(LM战士)

导航

EF做数据绑定时一些神奇问题

1.使用代码对绑定控件附值后需要调用BindingSource1.EndEdit(),以提交编辑到对应的数据源

2.有总数、金额、单价三个输TextBox,结果发现每次切换表格选中行时,记录老变成修改状态,找了半天发现是因为
在总数,金额的TextChanged中会自动计算单价并其设置txtUnitPrice.Text=xxx.xx,引起的


解决代码

                decimal amount = 0;
                decimal totalMoney = 0;
                if (decimal.TryParse(amountTextBox.Text, out amount)
                   && decimal.TryParse(totalMoneyTextBox.Text, out totalMoney)
                   && amount != 0
                  )
                {
                    var curV= (totalMoney / amount).ToString("0.00");

                    var curObj = BindingSrc.Current as BuyRecord;
                    if (curObj != null && curObj.UnitPrice.HasValue)
                    {
                        if (curObj.UnitPrice.Value.ToString("0.00") == curV)
                        {
                            return;
                        }
                    }
                    unitPriceTextBox.Text = curV;
                    this.buyRecordBindingSource.EndEdit();

                }

posted on 2012-08-22 16:00  遗忘海岸  阅读(324)  评论(0编辑  收藏  举报