F
F
FAwafawf12122021-06-17 06:30:43
C++ / C#
FAwafawf1212, 2021-06-17 06:30:43

How can I merge multiple columns into one datagrid for further editing?

I am working on visual studio, C#, WPF, SQL database. I have a datagrid and one big problem. I need to display data from multiple tables in a datagrid. In the first datagrid I have 4 fields, the first is a counter, the second int in it I write the cabinet number and the third with the fourth field - there are ints, IDs from other tables that I linked through the scheme to this table. The third column has a key that is associated with a table that contains information about employees, namely their full name, and so on. The fourth column goes to the key from the table, which contains information about the equipment (it comes in sets. The counter, the name of the set and its composition, which in turn consists of a bunch of other keys already connected to it, and they lead to tables with mice, keyboard and others) I need to make the datagrid display the following: Room number, Full name of the user (only the last name is possible), and brief information about the equipment that is included in the kit, if it is connected. The biggest problem is that I can merge through join, but then there will be problems with editing, this function does not work, so for now I settled on the following: I made it so that you can edit, delete, update datagrid rows, but so far only information about the cabinet number and IDs of the keys that are connected (I don’t know how to change it to the fields I need). Tell me how to implement it so that it is possible to perform the functions of editing, deleting, etc. Here is the C# code for the page where the datagrid is located. this function does not work, so I have settled on the following for now: I made it so that you can edit, delete, update the datagrid rows, but so far only information about the cabinet number and IDs of the keys that are connected is displayed in it (I don’t know how to convert it to the fields I need ). Tell me how to implement it so that it is possible to perform the functions of editing, deleting, etc. Here is the C# code for the page where the datagrid is located. this function does not work, so I have settled on the following for now: I made it so that you can edit, delete, update the datagrid rows, but so far only information about the cabinet number and IDs of the keys that are connected is displayed in it (I don’t know how to convert it to the fields I need ). Tell me how to implement it so that it is possible to perform the functions of editing, deleting, etc. Here is the C# code for the page where the datagrid is located.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
 
namespace YchetApparatov
{
  
    public partial class CabinetPage : Page
    {
        
        public CabinetPage()
        {
            InitializeComponent();
            DGCabinetViev.ItemsSource = NormApparatYchetEntities2.GetContext().CabinetTable.ToList();
 
        }
 
        private void UpdateCabinetClick(object sender, RoutedEventArgs e)
        {
            DGCabinetViev.ItemsSource = NormApparatYchetEntities2.GetContext().CabinetTable.ToList();
 
        }
 
        private void DeleteCabinetClick(object sender, RoutedEventArgs e)
        {
            var cabinetremoving = DGCabinetViev.SelectedItems.Cast<CabinetTable>().ToList();
 
            if (MessageBox.Show($" вы точно хотите удалить следующие {cabinetremoving.Count()} элементов?", "внимание",
                    MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes)
 
                try
                {
                    NormApparatYchetEntities2.GetContext().CabinetTable.RemoveRange(cabinetremoving);
                    NormApparatYchetEntities2.GetContext().SaveChanges();
                    MessageBox.Show("данные удалены");
 
                    DGCabinetViev.ItemsSource = NormApparatYchetEntities2.GetContext().CabinetTable.ToList();
                }
 
 
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message.ToString());
                }
        }
 
        private void AddNewCabinetClick(object sender, RoutedEventArgs e)
        {
            AddCabinetWindow addnewcabinet = new AddCabinetWindow(null);
            addnewcabinet.Show();
        }
 
        private void BtnEdit_Click(object sender, RoutedEventArgs e)
        {
            AddCabinetWindow addnewcabinet = new AddCabinetWindow((sender as Button).DataContext as CabinetTable);
            addnewcabinet.Show();
        }
    }
 
}


Here is his code, but already in XAML

<Page x:Class="YchetApparatov.CabinetPage"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
      xmlns:local="clr-namespace:YchetApparatov"
      mc:Ignorable="d" 
      d:DesignHeight="720" d:DesignWidth="1280"
      Title="CabinetPage">

    <Grid Margin="0,97,0,34">
        <Grid.RowDefinitions>
        </Grid.RowDefinitions>
        <DataGrid x:Name="DGCabinetViev" AutoGenerateColumns="False" IsReadOnly="True" Margin="0,-3,0,126">

            <DataGrid.Columns>
                <DataGridTextColumn Header="Номер кабинета" Binding="{Binding NomerCabineta}" Width="*" ></DataGridTextColumn>
                <DataGridTextColumn Header="Айди пользователя" Binding="{Binding IdPolzovatel}"  Width="*"></DataGridTextColumn>
                <DataGridTextColumn Header="Айди комплекта" Binding="{Binding IdComplect}" Width="*"></DataGridTextColumn>
              
                <DataGridTemplateColumn>
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <Button Content="Редактировать" Name="BtnEdit" Click="BtnEdit_Click" Canvas.Left="10" Canvas.Top="63" Width="107" Height="21"/>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
            </DataGrid.Columns>
        </DataGrid>
        <Button x:Name="AddNewCabinet" HorizontalAlignment="Left" Margin="7,-94,0,0" VerticalAlignment="Top" Width="51" Background="{x:Null}" Click="AddNewCabinetClick" Height="45">
            <StackPanel Orientation="Horizontal" Height="45" Width="51">
                <Image Source="images/Plus.png" Margin="0,0,5,6" Width="46" />

            </StackPanel>
        </Button>
        <Button x:Name="DeleteCabinet" HorizontalAlignment="Left" Margin="63,-94,0,0" VerticalAlignment="Top" Width="57" Click="DeleteCabinetClick" Background="{x:Null}" Height="45">
            <StackPanel Orientation="Horizontal" Height="45" Width="51">
                <Image Source="images/Krestik.png" Margin="0,0,5,5" Width="48" />
            </StackPanel>
        </Button>
        <Button Content="Очистить" HorizontalAlignment="Left" Margin="0,-30,0,0" VerticalAlignment="Top" Width="75"/>
        <TextBox HorizontalAlignment="Left" Height="22" Margin="88,-30,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="165"/>
        <Label Content="Фильтр" HorizontalAlignment="Left" Margin="0,-55,0,0" VerticalAlignment="Top" Height="25" Width="63"/>
        <Label Content="Поиск" HorizontalAlignment="Left" Margin="88,-55,0,0" VerticalAlignment="Top" Height="25" Width="63"/>
        <Button x:Name="UpdateCabinet" HorizontalAlignment="Left" Margin="125,-94,0,0" VerticalAlignment="Top" Width="51" Background="{x:Null}" Click="UpdateCabinetClick" Height="45">
            <StackPanel Orientation="Horizontal" Height="45" Width="51">
                <Image Source="images/Update.png" Margin="0,0,5,0" Width="47" />
            </StackPanel>
        </Button>
    </Grid>
</Page>

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question