检查 dataGrid 值时出现问题,如果它是真的,则更新它,否则添加新行



我是 WPF 的新手,我确实面临着绑定和 itemsource 的许多问题,并开始采用这种荒谬的方式来绑定控件,尤其是使用 dataGrid 我实际上习惯于 使用 winforms 编程,无论如何我有一个文本框获取了项目 ID 并使用 dataGrid 行检查它,如果有它的行之一具有相同的 ID,然后根据我已经在其他文本框(如 txtQty (中给出的值更新其列和txtBuyPrice,如果没有具有相同ID的行,则只需添加新行,如以下代码

private void btnAdd_Click(object sender, RoutedEventArgs e)
{
if (ItemID != "" && txtQty.Text != "" && int.Parse(txtQty.Text) > 0)
{
for (int i = 0; i < dataGridView.Items.Count; i++)
{
if (int.Parse(ItemID) == int.Parse((dataGridView.Items[i] as DataRowView).Row.ItemArray[0].ToString()))
{
(dataGridView.SelectedCells[3].Column.GetCellContent(i) as TextBlock).Text = int.Parse(txtQty.Text) +
int.Parse((dataGridView.SelectedCells[3].Column.GetCellContent(i) as TextBlock).Text).ToString();
(dataGridView.SelectedCells[4].Column.GetCellContent(i) as TextBlock).Text = txtBuyPrice.Text;
(dataGridView.SelectedCells[5].Column.GetCellContent(i) as TextBlock).Text = string.Format("{0:n2}",
double.Parse((dataGridView.SelectedCells[0].Column.GetCellContent(i) as TextBlock).Text) *
double.Parse((dataGridView.SelectedCells[0].Column.GetCellContent(i) as TextBlock).Text));                                                     
txtSearch.Focus();
return;
}
}
object date;
if (dtpExpireDate.IsEnabled == true)
date = dtpExpireDate.Text;
else
date = null;
var data = new AddItems { ID = ItemID, ItemName = txtItemName.Text,Date=date,
Qty=txtQty.Text,BuyPrice=txtBuyPrice.Text,
TotalPrice=string.Format("{0:n2}",(int.Parse(txtQty.Text)*double.Parse(txtBuyPrice.Text)).ToString())};
dataGridView.Items.Add(data);
ClearItem();
txtSearch.Focus();
}
}

参考数据来自以下函数

public class AddItems
{
public string ID { get; set; }
public string ItemName { get; set; }
public object Date { get; set; }
public string Qty { get; set; }
public string BuyPrice { get; set; }
public string TotalPrice { get; set; }
}

当 If 条件在 ItemID 和 dataGrid 行之间进行比较时,以及当 dataGrid 不为空并且旁边已经有一行时,如果 dataGrid为空,新行将正常添加,异常是(对象引用未设置为对象的实例(并且此异常指向第二个 if 语句,它是在 ItemID 和 dataGrid 值之间进行比较。 所以请我已经花了 20 个小时来解决,但我没有得到真正好的答案。

这不是一个正确的方法,但由于你的项目缺乏时间,我会尽力给你最好的。

首先,您必须重写 AddItems 类,如下所示:

public class AddItems : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string propertyName)
{
if(PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
private string id;
private string itemName;
private object date;
private decimal qty;
private decimal buyPrice;
private decimal totalPrice;
public string ID 
{
set
{
this.id = value;
this.OnPropertyChanged("ID");
}
get{return this.id;}
}
public string ItemName 
{
set
{
this.itemName = value;
this.OnPropertyChanged("ItemName");
}
get{return this.itemName;}
}
public object Date 
{
set
{
this.date = value;
this.OnPropertyChanged("Date");
}
get{return this.date;}
}
public decimal Qty 
{
set
{
this.qty = value;
this.OnPropertyChanged("Qty");
}
get{return this.qty;}
}
public decimal BuyPrice 
{
set
{
this.buyPrice = value;
this.OnPropertyChanged("BuyPrice");
}
get{return this.buyPrice;}
}
public decimal TotalPrice 
{
set
{
this.totalPrice = value;
this.OnPropertyChanged("TotalPrice");
}
get{return this.totalPrice;}
}
public void AddQuantity(decimal quantity)
{
this.qty = this.qty + quantity;
}
}

在类中添加其他逻辑以更新所需的所有列。

在 tour 主代码中,您可以构建一个ObservableCollection<AddItems> addItemsList = new ObservableCollection<AddItems>()集合,您可以在其中存储行数据。

填写列表后,您可以按如下方式绑定网格:dataGridView.ItemSource = addItemsList;

之后,您可以运行任何代码,进行检查,直接在网格的列表中,更改所需的值等。所有更改都显示在网格上,无需额外编码。

相关内容

最新更新