【问题标题】:Problems with checking dataGrid values if it's true then update it else add new row检查dataGrid值的问题,如果它是真的然后更新它,否则添加新行
【发布时间】:2017-08-24 20:24:58
【问题描述】:

我是 WPF 的新手,我在绑定和 itemsource 方面确实面临许多问题,并开始讨厌这种荒谬的绑定控件的方式,尤其是使用 dataGrid 我实际上习惯于使用 winforms 进行编程,无论如何我有一个 textBox项目 ID 并使用 dataGrid Rows 检查它,如果其中一个行具有相同的 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; }
    }

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

【问题讨论】:

  • WPF 是一种很好的开发方式,但您必须使用 MVVM 才能实现数据绑定的最大效率。
  • @DimosK 我真的需要尽快开发这个项目,所以我没有时间学习 MVVM,但如果你有课程或书籍,请提及跨度>
  • 网上有很多东西要读。反正。如何绑定数据网格?你有清单吗?
  • @DimosK 没有绑定我只是从文本框中获取值并通过 AddItems 类将其放入 dataGrid 中,我不知道这是否正确,因为我不熟悉绑定事情,如果不正确,我应该怎么做才能让事情继续写下去,我真的很困惑。
  • 您目前使用的是 WPF 还是 WinForms?完整的数据网格类型名称是什么?

标签: c# wpf datagrid


【解决方案1】:

这不是正确的方法,但由于您的项目没有时间,我会尽力为您提供最好的。

首先你必须重写 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;
    }
}

在类中添加额外的逻辑来更新你需要的所有列。

在游览主代码中,您可以构建一个 ObservableCollection&lt;AddItems&gt; addItemsList = new ObservableCollection&lt;AddItems&gt;() 集合,您可以在其中存储您的行数据。

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

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

【讨论】:

  • 我正在像您所做的那样做先生,但什么也没发生,dataGrid 中没有显示任何行我认为我做错了我什至不知道什么数据绑定或 itemsource 方法.
  • 我确定将添加整个值,但它不会显示在 dataGrid 中,所以我应该怎么做才能使其显示,除了如何迭代所有此列表的值以获得用于修改的特定值例如,我需要知道哪个单元格具有相同的 ItemID(ItemID 来自我从某个文本框输入的内容)如果有一个单元格具有相同的值然后更新整行
猜你喜欢
  • 1970-01-01
  • 2020-05-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-09-13
  • 1970-01-01
  • 2019-10-13
  • 2020-02-20
相关资源
最近更新 更多