开发者

Multiplication Silverlight Datagrid

I have a Silverlight datagrid that allows the use to enter in how many of a product开发者_JAVA技巧 that you plan to order. Already populated is he cost of the item. Right now I have an event that once you finish editing t he a cell it checks that header, but how do I multiply those two cells and place the value in a cell in the cost column. I know I would really be changing the List of the objects that is bound to my datagrid but I don't know how to do this.

 client.GetOrderCompleted += (s, ea) =>
        {
            dgOrder.AutoGenerateColumns = false;
            //dgOrder.ColumnWidth.Value = 100;





            dgOrder.Columns.Add(CreateTextColumn("SKU", "SKU"));
            dgOrder.Columns.Add(CreateTextColumn("productname", "Product Name"));
            dgOrder.Columns.Add(CreateTextColumn("itemnumber", "Item Number"));
            dgOrder.Columns.Add(CreateTextColumn("cost", "Cost"));
            dgOrder.Columns.Add(CreateTextColumn("asin", "ASIN"));
            dgOrder.Columns.Add(CreateTextColumn("pendingorder", "Rank"));
            dgOrder.Columns.Add(CreateTextColumn("rank", "Node"));
            //dgOrder.Columns.Add(CreateTextColumn("w4", "AMZN"));
            dgOrder.Columns.Add(CreateTextColumn("amazon", "AMZN"));
            dgOrder.Columns.Add(CreateTextColumn("ourprice", "OurPrice"));
            dgOrder.Columns.Add(CreateTextColumn("bbprice", "BuyBox"));
            dgOrder.Columns.Add(CreateTextColumn("afner", "AFN"));
            dgOrder.Columns.Add(CreateTextColumn("quantity", "INV"));
            dgOrder.Columns.Add(CreateTextColumn("w4", "W4"));
            dgOrder.Columns.Add(CreateTextColumn("w3", "W3"));
            dgOrder.Columns.Add(CreateTextColumn("w2", "W2"));
            dgOrder.Columns.Add(CreateTextColumn("w1", "W1"));
            dgOrder.Columns.Add(CreateTextColumn("order", "Order"));
            dgOrder.Columns.Add(CreateTextColumn("total", "Total"));
            dgOrder.Columns.Add(CreateTextColumn("profit", "Profit"));
            dgOrder.Columns.Add(CreateTextColumn("percent", "Percent"));
            dgOrder.Columns.Add(CreateHyperlink("asin"));
            dgOrder.ItemsSource = ea.Result;
            Original = ea.Result;

        };
    client.GetOrderAsync(txtBox.Text);

XAML:

<sdk:DataGrid AutoGenerateColumns="True" 
      Height="469" 
      HorizontalAlignment="Left" 
      Margin="12,41,0,0" 
      Name="dgOrder" 
      VerticalAlignment="Top" 
      Width="1316" 
      HeadersVisibility="All" 
      CellEditEnded="dgOrder_CellEditEnded" 
      RowBackground="DodgerBlue" 
      AlternatingRowBackground="LightBlue"
      LostFocus="TextBox_LostFocus" 
      BeginningEdit="dgOrder_BeginningEdit" 
      SelectionChanged="dgOrder_SelectionChanged"
      LoadingRow="dgOrder_LoadingRow" />


You could modify the object bound to your datagrid and include a property for the total cost. For example, assuming that your DataGrid is bound to an IList you can add a new TotalCost property:

public class ItemOrder
{
    public double TotalCost
    {
        get
        {
            return this.OrderAmount * this.ItemCost;
        }
    }

    // rest of your class
}

Alternatively, you could use a value converter to display the total cost:

<DataGrid>
    <DataGrid.Resources>
        <TotalCostConverter x:Key="TotalCostConverter"/>
    </DataGrid.Resources>
    <DataGrid.Columns>
        <DataGridTextColumn Header="Amount" Binding="{Binding OrderAmount}"/>
        <DataGridTextColumn Header="ItemCost" Binding="{Binding ItemCost}"/>
        <DataGridTextColumn Header="Total" Binding="{Binding Converter={StaticResource TotalCostConverter}"/>
    </DataGrid.Columns>
</DataGrid>


//Converter
public class TotalCostConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        var itemOrder = value as ItemOrder;
        return itemOrder.OrderAmount * itemOrder.ItemCost;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

Edit: After looking at your code, you can add a new column to the data grid binded to your ItemOrder object and attatch the value converter to it.

dgOrder.Columns.Add(
    new DataGridTextColumn()
    { 
        Header = "Total",
        Binding = new Binding() 
        {
            //The Value converter described above
            Converter = new TotalCostConverter(),             
        }
    }        

);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜