samedi 14 décembre 2019

How to make CheckBoxex in DataGridView?

There is my project. It seems like some app to buy Electronics. I want to paste CheckBox into a column in DataGridView and have it add the selected row to the new List Cart. How to make right Event Handler for those CheckBoxes? There is code:

namespace MyFinalProject{

public partial class MainWindow : Window
{
    List<ElAppliance> appliances = new List<ElAppliance>();
    List<ElAppliance> cart = new List<ElAppliance>();

    public MainWindow()
    {
        const int NUMBER_OF_GOODS = 7;

        using (StreamReader reader = new StreamReader("Storage.txt"))
        {
            string tmpAppliances;
            string[] separated;
            string[] type = new string[NUMBER_OF_GOODS];
            for (int i = 0; i < NUMBER_OF_GOODS; ++i)
            {
                tmpAppliances = reader.ReadLine();
                char splitter = '|';
                separated = tmpAppliances.Split(splitter);
                try
                {
                    if (separated[0].StartsWith("VacuumCleaner"))
                    {
                        appliances.Add(new VacCleaner(separated[1], separated[2], Convert.ToDouble(separated[3]),
                            Convert.ToDouble(separated[4]), separated[5]));
                    }
                    else if (separated[0].StartsWith("WashingMachine"))
                    {
                        appliances.Add(new WashingMachine(separated[1], separated[2], Convert.ToDouble(separated[3]),
                            Convert.ToInt32(separated[4]), Convert.ToDouble(separated[5])));
                    }
                    else
                    {
                        appliances.Add(new FoodProcessor(separated[1], separated[2], Convert.ToDouble(separated[3]),
                            Convert.ToDouble(separated[4]), Convert.ToInt32(separated[5])));
                    }
                    type[i] = separated[0];
                }
                catch (Exception ex)
                {
                    MessageBox.Show($"{ex.Message}");
                }

            }
        }
        int length = appliances.Count;
        for (int i = 0; i < appliances.Count; i++)
        {
            for (int j = i+1; j < appliances.Count; j++)
            {
                    if(appliances[i].Name == appliances[j].Name && appliances[j].Number == 1)
                    {
                        appliances[j].Number++;
                        appliances.Remove(appliances[i]);
                    }
            }
        }
        appliances.Sort();
        InitializeComponent();

        foreach (var appliance in appliances)
        {
            if(appliance.GetType() == typeof(VacCleaner))
            vacuumGrid.Items.Add(appliance);
            if (appliance.GetType() == typeof(FoodProcessor))
                foodGrid.Items.Add(appliance);
            if (appliance.GetType() == typeof(WashingMachine))
                washingGrid.Items.Add(appliance);
        }


    }

}
public class ElAppliance : IComparable<ElAppliance>
{

    string name;
    string company;
    double price;
    public int number = 1;
    CheckBox chBox = new CheckBox();

    public CheckBox ChBox { get { return this.chBox; } set { this.chBox = value; } }
    public string Name { get { return this.name; } }
    public string Company { get { return this.company; } }
    public double Price { get { return this.price; } }
    public int Number { get { return this.number; } set { this.number = value; } }

    public ElAppliance(string name, string company, double price)
    {
        this.name = name;
        this.company = company;
        this.price = price;
    }

    public int CompareTo(ElAppliance other)
    {
        return this.name.CompareTo(other.name);
    }
    public override string ToString() => ($"Name: {name} | Company : {company} | Price: {price}");

}
public class VacCleaner : ElAppliance
{

    double power;
    string color;
    public double Power { get { return this.power; } }
    public string Color { get { return this.color; } }
    public VacCleaner(string name, string company, double price, double power, string color) : base(name, company, price)
    {
        this.power = power;
        this.color = color;
    }
    public override string ToString()
    {
        return base.ToString() + ($" | Power: {power} | Color: {color}");
    }

}
public class WashingMachine : ElAppliance
{
    int numbOfPrograms;
    double volume;
    public int NumbOfPrograms { get { return this.numbOfPrograms; } }
    public double Volume { get { return this.volume; } }
    public WashingMachine(string name, string company, double price, int numbOfPrograms, double volume) : base(name, company, price)
    {
        this.numbOfPrograms = numbOfPrograms;
        this.volume = volume;
    }
    public override string ToString()
    {
        return base.ToString() + ($" | Number of Programs: {numbOfPrograms} | Volume: {volume}");
    }
}
class FoodProcessor : ElAppliance
{
    double power;
    int numbOfFuncs;
    public double Power
    {
        get
        {
            return this.power;
        }
    }
    public int NumbOfFuncs
    {
        get
        {
            return this.numbOfFuncs;
        }
    }
    public FoodProcessor(string name, string company, double price, double power, int numbOfFuncs) : base(name, company, price)
    {
        this.power = power;
        this.numbOfFuncs = numbOfFuncs;
    }
    public override string ToString()
    {
        return base.ToString() + ($" | Power: {power} | Number of Functions: {numbOfFuncs}");
    }
}

XAML code:`

            <DataGrid.Items>

            </DataGrid.Items>
            <DataGrid.Columns>
                <DataGridTextColumn Header="Model" Binding="{Binding Path=Name}" Width="90"  IsReadOnly="True"/>
                <DataGridTextColumn Header="Company" Binding="{Binding Path=Company}" Width="80" IsReadOnly="True"/>
                <DataGridTextColumn Header="Price" Binding="{Binding Path=Price}" Width="50" IsReadOnly="True"/>
                <DataGridTextColumn Header="Power" Binding="{Binding Path=Power}" Width="50" IsReadOnly="True"/>
                <DataGridTextColumn Header="Color" Binding="{Binding Path=Color}" Width="50" IsReadOnly="True"/>
                <DataGridTextColumn Header="Number" Binding="{Binding Path=Number}" Width="50" IsReadOnly="True"/>
                <DataGridCheckBoxColumn Header="Add" Binding="{Binding Path=ChBox}" Width="50" IsReadOnly="False"/>

            </DataGrid.Columns>

            <DataGrid.RowDetailsTemplate>
                <DataTemplate>
                    <StackPanel x:Name="stackP" Orientation="Horizontal">

                    </StackPanel>
                </DataTemplate>
            </DataGrid.RowDetailsTemplate>

        </DataGrid>
        <Label Name="foods" Content="Food Processors:" HorizontalAlignment="Left" FontSize="13"/>
        <DataGrid x:Name="foodGrid" AutoGenerateColumns="False" HorizontalGridLinesBrush="DarkGray"
RowBackground="LightGray" AlternatingRowBackground="White">

            <DataGrid.Items>

            </DataGrid.Items>
            <DataGrid.Columns>
                <DataGridTextColumn Header="Model" Binding="{Binding Path=Name}" Width="90" IsReadOnly="True"/>
                <DataGridTextColumn Header="Company" Binding="{Binding Path=Company}" Width="80" IsReadOnly="True"/>
                <DataGridTextColumn Header="Price" Binding="{Binding Path=Price}" Width="50" IsReadOnly="True" />
                <DataGridTextColumn Header="Power" Binding="{Binding Path=Power}" Width="50" IsReadOnly="True"/>
                <DataGridTextColumn Header="Functions" Binding="{Binding Path=NumbOfFuncs}" Width="50" IsReadOnly="True" />
                <DataGridTextColumn Header="Number" Binding="{Binding Path=Number}" Width="50" IsReadOnly="True" />
            </DataGrid.Columns>

            <DataGrid.RowDetailsTemplate>
                <DataTemplate>
                    <StackPanel x:Name="stackP" Orientation="Horizontal">

                    </StackPanel>
                </DataTemplate>
            </DataGrid.RowDetailsTemplate>

        </DataGrid>
        <Label Name="washings" Content="Washing Machines:" HorizontalAlignment="Left" FontSize="13"/>
        <DataGrid x:Name="washingGrid" AutoGenerateColumns="False" HorizontalGridLinesBrush="DarkGray"
RowBackground="LightGray" AlternatingRowBackground="White" Margin="0,0,-0.4,0">

            <DataGrid.Columns>
                <DataGridTextColumn Header="Model" Binding="{Binding Name}" Width="90" IsReadOnly="True"/>
                <DataGridTextColumn Header="Company" Binding="{Binding Company}" Width="80" IsReadOnly="True"/>
                <DataGridTextColumn Header="Price" Binding="{Binding Price}" Width="50" IsReadOnly="True"/>
                <DataGridTextColumn Header="Programs" Binding="{Binding Path=NumbOfPrograms}" Width="50" IsReadOnly="True"/>
                <DataGridTextColumn Header="Volume" Binding="{Binding Path=Volume}" Width="50" IsReadOnly="True"/>
                <DataGridTextColumn Header="Number" Binding="{Binding Path=Number}" Width="55" IsReadOnly="True"/>
            </DataGrid.Columns>

            <DataGrid.RowDetailsTemplate>
                <DataTemplate>
                    <StackPanel x:Name="stackP" Orientation="Horizontal"/>
                </DataTemplate>
            </DataGrid.RowDetailsTemplate>

        </DataGrid>
    </StackPanel>
</Grid>`

Sorry for mistakes. Pls help me finish this project:)




Aucun commentaire:

Enregistrer un commentaire