简体   繁体   中英

update cell in datagrid wpf

I have an invoice program that keeps track of items selected that the customer may want to buy. every time that the "add item" button is pushed, the QTY cell increments. The problem im running in to is that the value is getting upgraded in the background, but is not being changed in the cell itself. If you click on the cell however, the value will update. But i want it to update as soon as i click that "add item" button. What can i do to update the cell information as i click this button? this is the code i have that works with the button push

public partial class MainWindow : Window
}
private void btnAddItem_Click(object sender, RoutedEventArgs e)
    {
        addDataGridRow(invoiceItemID, cbxItems.SelectedItem.ToString(), Convert.ToDouble(txtPrice.Text));
    }

    private void addDataGridRow(int id, string itemName, double itemPrice)
    {
        //lstDataItem.Add(new DataItem { sQTY = count.ToString(), sNAME = itemName, sPRICE = itemPrice, sRemove = "Delete Item" });
       // Rows = new ObservableCollection<DataItem> { new DataItem () };
        for (int i = 0; i < Rows.Count; i++)
        {
            if (Rows[i].sNAME == itemName)
            {
                Rows[i].sQTY = Rows[i].sQTY + 1;
                id = Rows[i].id;
                bSameItem = true;
            }
        }
        if (id == 0)
        {
            Rows.Add(new DataItem());
        }
        else if (!bSameItem)
        {
            Rows.Add(new DataItem());
        }


        if (!bSameItem)
        {
            Rows[id].id = id;
            Rows[id].sQTY = 1;
            Rows[id].sNAME = itemName;
            Rows[id].sPRICE = itemPrice;
            invoiceItemID++;
            dgInvoice.ItemsSource = Rows;

        }
        bSameItem = false;
    }
}

public class DataItem
{
    public int id { get; set; }
    public int sQTY { get; set; }
    public string sNAME { get; set; }
    public double sPRICE { get; set; }
}

i dont know if you need my xaml but here is it just in case

        <DataGrid x:Name="dgInvoice" HorizontalAlignment="Left" Margin="10,92,0,0" VerticalAlignment="Top" Height="608" Width="488" AutoGenerateColumns="False" ItemsSource="{Binding}" RowHeight="25">
            <DataGrid.Columns>
                <DataGridTextColumn Header="QTY" Binding="{Binding Path=sQTY}" Width="100"/>
                <DataGridTextColumn Header="NAME" Binding="{Binding Path=sNAME}" Width="200"/>
                <DataGridTextColumn Header="PRICE" Binding="{Binding Path=sPRICE}" Width="80"/>
                <DataGridTemplateColumn Width="100">
                    <DataGridTemplateColumn.CellEditingTemplate>
                        <DataTemplate>
                            <Button Height="20">Delete Item</Button>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellEditingTemplate>
                </DataGridTemplateColumn>
            </DataGrid.Columns>
        </DataGrid>

there is not many responses to wpf that i have seen when i have searched this topic. Any help would be greatly appreciated.

You have to implement INotifyPropertyChanged interface on your class DataItem so that any changes in its property gets reflected back on UI.

public class DataItem : INotifyPropertyChanged
{
   public event PropertyChangedEventHandler PropertyChanged;

   private int id;
   public int Id
   {
      get { return id; }
      set
      {
         if(id != value)
         {
            id= value;
            OnPropertyChanged("Id");
         }
      }
   }

  // Create the OnPropertyChanged method to raise the event 
  protected void OnPropertyChanged(string name)
  {
     PropertyChangedEventHandler handler = PropertyChanged;
     if (handler != null)
     {
        handler(this, new PropertyChangedEventArgs(name));
     }
  }
}

Make other properties to follow the same syntax like above for Id and you will see any update in property value will reflect back on GUI.

我只需要做dgInvoices.Items.Refresh();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM