下面给大家写一个修改数据的方法。为了修改客户需要的数据,同时还满足客户的数据要求,我们就要通过修改的方法来实现这一个功能,具体实现模糊筛选的方法如下:
首先查询出全部的数据然后在全部的数据中进行模糊筛选,将要修改的数据回填出来。从中修改自己想要修改的数据。
下面是实现查询的代码:
查询全部表格信息:
/// <summary>
/// 查询表格数据信息
/// </summary>
private void SelectCommodity()
{
list = (from tbCommodity in myModel.Commodity
join tbUint in myModel.Unit on tbCommodity.UnitID equals tbUint.UnitID
join tbCommodityType in myModel.CommodityType on tbCommodity.CommodityTypeID equals tbCommodityType.CommodityTypeID
join tbSuppliers in myModel.Suppliers on tbCommodity.SuppliersID equals tbSuppliers.SuppliersID
join tbFrim in myModel.Firm on tbCommodity.FirmID equals tbFrim.FirmID
join tbType in myModel.Type on tbSuppliers.TypeID equals tbType.TypeID
orderby tbCommodity.CommodityID descending
select new CommodityEntity
{
CommodityID = tbCommodity.CommodityID,
UnitID = tbUint.UnitID,//
CommodityTypeID = tbCommodity.CommodityTypeID,//商品ID
SuppliersID = tbCommodity.SuppliersID,//供应商ID
TypeID = tbSuppliers.TypeID,//禁用类型ID
FirmID = tbCommodity.FirmID,//厂商id
BarCode = tbCommodity.BarCode,//商品条码
StoreCode = tbCommodity.StoreCode,//
Count = tbCommodity.Count,//数量
ChargePrice = tbCommodity.ChargePrice,//进货
WholesalePrice = tbCommodity.WholesalePrice,//批发价
RetailPrice = tbCommodity.RetailPrice,//零售价
CommodityName = tbCommodity.CommodityName,//商品名称
SuppliersCode = tbSuppliers.SuppliersCode,//供应商编码
SuppliersName = tbSuppliers.SuppliersName,//供应商名称
FirmName = tbFrim.FirmName,//厂商名称
GuiGe = tbCommodity.GuiGe,//规格
CommodityTypeCode = tbCommodityType.CommodityTypeCode,//商品类别编码
Guarantee = tbCommodity.Guarantee,//保质期
Habitat = tbCommodity.Habitat,//厂地
Remark = tbCommodity.Remark,//备注
TypeName = tbType.TypeName,
UnitName = tbUint.UnitName,
}).ToList();
dgCommod.ItemsSource = list;
}
下面是回填的方法,通过上面查询出来的数据,然后选择要修改的那一条数据,双击它,然后回填到下面的文本框中,如下图:
具体的实现代码如下:
/// <summary>
/// 双击回填页面数据
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void DgCommod_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
//启用修改按钮
btn_Update.IsEnabled = true;
btn_Update.Opacity = 1;
#region 数据回填
//获取当前选中行的对象
CommodityEntity myCommodity = dgCommod.CurrentItem as CommodityEntity;
BarCode.Text = myCommodity.BarCode;
StoreCode.Text = myCommodity.StoreCode;
ChargePrice.Text = Convert.ToInt32(myCommodity.ChargePrice).ToString();
WholesalePrice.Text = Convert.ToInt32(myCommodity.WholesalePrice).ToString();
RetailPrice.Text = Convert.ToInt32(myCommodity.WholesalePrice).ToString();
CommodityName.Text = myCommodity.CommodityName;
SuppliersCode.Text = myCommodity.SuppliersCode;
Count.Text = myCommodity.Count;
GuiGe.Text = myCommodity.GuiGe;
CommodityTypeCode.Text = myCommodity.CommodityTypeCode;
GuiGe.Text = myCommodity.GuiGe;
FirmName.Text = myCommodity.FirmName;
UnitName.Text = myCommodity.UnitName;
TypeName.Text = myCommodity.TypeName;
Guarantee.Text = myCommodity.Guarantee;
Habitat.Text = myCommodity.Habitat;
Remark.Text = myCommodity.Remark;
SuppliersName.Text = myCommodity.SuppliersName;
#endregion
}
修改完上面的数据之后,下面是提交修改的方法,将要修改的数据上传到数据库中,然后在把数据返回到页面,实现修改的功能。
/// <summary>
/// 修改按钮
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Update_Click(object sender, RoutedEventArgs e)
{
try
{
Commodity myCommodity = dgCommod.SelectedItem as Commodity;
if (myCommodity != null)
{
if (Convert.ToInt32(CommodityTypeCode.SelectedValue) != 0 && Convert.ToInt32(TypeName.SelectedValue) != 0 && Convert.ToInt32(UnitName.SelectedValue) != 0
&& Convert.ToInt32(FirmName.SelectedValue) != 0 && SuppliersCode.Text.Trim() != String.Empty && BarCode.Text.Trim() != String.Empty
&& StoreCode.Text.Trim() != String.Empty && Guarantee.Text.Trim() != String.Empty && Remark.Text.Trim() != String.Empty)
{
var TBCommofity = myModel.Commodity.Where(x => x.CommodityID == myCommodity.CommodityID).SingleOrDefault();
//实例化当前的表
//Commodity TBCommofity = new Commodity();
//Suppliers mySuppliers = new Suppliers();
TBCommofity.BarCode = BarCode.Text.Trim();
TBCommofity.StoreCode = StoreCode.Text.Trim();
TBCommofity.ChargePrice = Convert.ToInt32(ChargePrice.Text.Trim().ToString());
TBCommofity.WholesalePrice = Convert.ToInt32(WholesalePrice.Text.Trim().ToString());
TBCommofity.RetailPrice = Convert.ToInt32(RetailPrice.Text.Trim().ToString());
TBCommofity.GuiGe = GuiGe.Text.Trim();
TBCommofity.Guarantee = Guarantee.Text.Trim();
TBCommofity.Habitat = Habitat.Text.Trim();
TBCommofity.Count = Count.Text.Trim();
TBCommofity.Remark = Remark.Text.Trim();
TBCommofity.CommodityName = CommodityName.Text.Trim();
TBCommofity.UnitID = Convert.ToInt32(UnitName.SelectedValue);
TBCommofity.TypeID = Convert.ToInt32(TypeName.SelectedValue);
TBCommofity.SuppliersID = Convert.ToInt32(SuppliersCode.SelectedValue);
TBCommofity.FirmID = Convert.ToInt32(FirmName.SelectedValue);
TBCommofity.CommodityTypeID = Convert.ToInt32(CommodityTypeCode.SelectedValue);
myModel.Entry(TBCommofity).State = System.Data.Entity.EntityState.Modified;
if (myModel.SaveChanges() > 0)
{
MessageBox.Show("修改数据成功!", "提示", MessageBoxButton.OK, MessageBoxImage.Information);
//清空页面
EmptyControls();
//刷新表格
SelectCommodity();
// 禁用按钮
btn_Update.IsEnabled = false;
btn_Update.Opacity = 0.6;
}
else
{
MessageBox.Show("修改数据失败!", "提示", MessageBoxButton.OK, MessageBoxImage.Information);
}
} else{
MessageBox.Show("请把信息填写完整", "提示", MessageBoxButton.OKCancel, MessageBoxImage.Information);
}
}
}
catch (Exception)
{
throw;
}
}
修改的具体数据如下图: