DataGrid乘以两列 [英] DataGrid Multiplicating Two Columns

查看:52
本文介绍了DataGrid乘以两列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要社区的帮助.我正在尝试将DataGrid中的两列(WPF和C#)的值相乘,第一列是从MySql数据库获取其数据,第二列是输入值,用户将在其中输入一个数字,该数字应与第一列,结果应显示在名为总计"的第三列中.我搜索了所有内容,并尝试了与尝试过几乎相同方法的其他人不同的方法,但我只是无法获得要乘的值,而结果也无法显示在第三列中.这是我尝试过的最后一部分代码,不得不提的是,我对C#和WPF还是很陌生,没有太多经验:

I'm in need of the community's help. I am trying to multiply the values of two columns in a DataGrid (WPF and C#), the first column get its data from a MySql database and the second column is an input value where a user will type a number which should be multiplied with the first column and the result should be displayed in a third column called "Total". I have searched all over and tried different approaches from other people who tried the nearly same thing but I just can't get the value to multiply and the result to appear in the third column. Here's the last bit of code I've tried, I have to mention that I am still very new to C# and WPF with not so much experience:

<DataGrid AutoGenerateColumns="False" x:Name="tblData" Margin="30,197,7,0" Grid.Row="1" VerticalAlignment="Top" Height="510" Grid.ColumnSpan="4"
              BorderThickness="2" BorderBrush="#FF445BBF" ItemsSource="{Binding Path=LoadDataBinding}" CanUserResizeRows="False" ClipToBounds="True"
              CanUserSortColumns="False" HorizontalGridLinesBrush="#FFC7C7C7" VerticalGridLinesBrush="#FFC7C7C7" IsManipulationEnabled="True" EnableRowVirtualization="False" 
              IsTextSearchEnabled="True" xmlns:local="clr-namespace:PoS_Pimentel">
        <DataGrid.Resources>
            <local:AmmountConverter x:Key="AmmountConverter" />
        </DataGrid.Resources>
        <DataGrid.Columns>
            <DataGridTextColumn Binding="{Binding Path=nomprod}" Header="Producto" Width="500" IsReadOnly="True">
                <DataGridTextColumn.HeaderStyle>
                    <Style TargetType="DataGridColumnHeader">
                        <Setter Property="HorizontalContentAlignment" Value="Center" />
                        <Setter Property="FontSize" Value="14" />
                    </Style>
                </DataGridTextColumn.HeaderStyle>
            </DataGridTextColumn>
            <DataGridTextColumn Binding="{Binding Path=preciogram, Mode=TwoWay}" Header="Precio por Gramo" Width="190" IsReadOnly="True">
                <DataGridTextColumn.HeaderStyle>
                    <Style TargetType="DataGridColumnHeader">
                        <Setter Property="HorizontalContentAlignment" Value="Center" />
                        <Setter Property="FontSize" Value="14" />
                    </Style>
                </DataGridTextColumn.HeaderStyle>
            </DataGridTextColumn>
            <DataGridTextColumn Binding="{Binding Path=gramos, Mode=TwoWay}" Header="Gramos" Width="190" IsReadOnly="False">
                <DataGridTextColumn.HeaderStyle>
                    <Style TargetType="DataGridColumnHeader">
                        <Setter Property="HorizontalContentAlignment" Value="Center" />
                        <Setter Property="FontSize" Value="14" />
                    </Style>
                </DataGridTextColumn.HeaderStyle>
            </DataGridTextColumn>
            <DataGridTextColumn Binding="{Binding Path=total, Mode=TwoWay}" Header="Total" Width="*" IsReadOnly="True">
                <DataGridTextColumn.HeaderStyle>
                    <Style TargetType="DataGridColumnHeader">
                        <Setter Property="HorizontalContentAlignment" Value="Center" />
                        <Setter Property="FontSize" Value="14" />
                    </Style>
                </DataGridTextColumn.HeaderStyle>
            </DataGridTextColumn>
        </DataGrid.Columns>
    </DataGrid>

在C#端,我为EntitiyClass和AmmountConverter类创建了两个单独的.cs文件:

and on the C# end I created two separate .cs file for an EntitiyClass and an AmmountConverter class:

EntityClass代码:

EntityClass code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ComponentModel;
using System.Collections.ObjectModel;
using System.Windows.Forms;

namespace PoS
{
    #region
    public class Entity_Class : INotifyPropertyChanged
    {
        private int _preciogram;
        public int PrecioGram
        {
            get { return _preciogram; }
            set { _preciogram = value; NotifyPropertyChanged("gramos"); }
        }

        private int _gramos;
        public int Gramos
        {
            get { return _gramos; }
            set { _gramos = value; NotifyPropertyChanged("gramos"); }
        }

        public event PropertyChangedEventHandler PropertyChanged;
        private void NotifyPropertyChanged(string propertyName)
        {
            if(PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }
}

和AmmountConverter类:

And the AmmountConverter class:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Data;

namespace PoS_Pimentel
{
    public class AmmountConverter : IMultiValueConverter
    {
        public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            double prcgrms = values[1] == null ? 0 : System.Convert.ToDouble(values[1]);
            double grms = values[2] == null ? 0 : System.Convert.ToDouble(values[2]);

            return prcgrms * grms;
        }

        public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}

我不是很擅长此事,但是我正在尝试,任何指针将不胜感激.谢谢大家.

I'm not very good at this but I am trying and any pointers will be greatly appreciated. Thank you all.

推荐答案

您无需使用转换器即可进行此计算.希望您可以在模型类中处理它.另外,您需要绑定到属性.DataGrid绑定属性名称不正确c#区分大小写.请参阅下面的代码.

You dont need to use a converter for doing this calculation. Hope you can handle it in Model Class. Also You need to bind to property. DataGrid binding property Names are not correct c# is case sensitive. Refer my below code.

 <DataGrid x:Name="dgr" AutoGenerateColumns="False" ItemsSource="{Binding LoadDataBinding}">           
        <DataGrid.Columns>                
            <DataGridTextColumn Binding="{Binding Path=PrecioGram, Mode=TwoWay}" Header="Precio por Gramo" Width="190" IsReadOnly="True">
                <DataGridTextColumn.HeaderStyle>
                    <Style TargetType="DataGridColumnHeader">
                        <Setter Property="HorizontalContentAlignment" Value="Center" />
                        <Setter Property="FontSize" Value="14" />
                    </Style>
                </DataGridTextColumn.HeaderStyle>
            </DataGridTextColumn>
            <DataGridTextColumn Binding="{Binding Path=Gramos, Mode=TwoWay}" Header="Gramos" Width="190" IsReadOnly="False">
                <DataGridTextColumn.HeaderStyle>
                    <Style TargetType="DataGridColumnHeader">
                        <Setter Property="HorizontalContentAlignment" Value="Center" />
                        <Setter Property="FontSize" Value="14" />
                    </Style>
                </DataGridTextColumn.HeaderStyle>
            </DataGridTextColumn>
            <DataGridTextColumn Header="Total" Width="100" Binding="{Binding Total}">
                <DataGridTextColumn.HeaderStyle>
                    <Style TargetType="DataGridColumnHeader">
                        <Setter Property="HorizontalContentAlignment" Value="Center" />
                        <Setter Property="FontSize" Value="14" />
                    </Style>
                </DataGridTextColumn.HeaderStyle>

            </DataGridTextColumn>
        </DataGrid.Columns>
    </DataGrid>

 public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        this.DataContext = new MainViewModel();
    }

}

public class MainViewModel
{
    private ObservableCollection<Entity_Class> myVar = new ObservableCollection<Entity_Class>();

    public ObservableCollection<Entity_Class> LoadDataBinding
    {
        get { return myVar; }
        set { myVar = value; }
    }

    public MainViewModel()
    {
        for (int i = 1; i < 10; i++)
        {
            LoadDataBinding.Add(new Entity_Class() { PrecioGram=i});
        }
    }
}   

public class Entity_Class : INotifyPropertyChanged
{
    private int _preciogram;
    public int PrecioGram
    {
        get { return _preciogram; }
        set { _preciogram = value; NotifyPropertyChanged("PrecioGram"); }
    }

    private int _gramos;
    public int Gramos
    {
        get { return _gramos; }
        set
        { 
            _gramos = value; NotifyPropertyChanged("gramos");
            Total = _preciogram * _gramos;
        }
    }

    private int _total;
    public int Total
    {
        get { return _total; }
        set { _total = value; NotifyPropertyChanged("Total"); }
    }

    public event PropertyChangedEventHandler PropertyChanged;
    private void NotifyPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

这篇关于DataGrid乘以两列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆