Answer the question
In order to leave comments, you need to log in
How to implement the datagrid editing function if the tables are merged?
I am working on WPF C# visual studio.
I need to display two tables in one (I did it). After that, you need to edit the contents of the table, I have a form in a new window to add new data, I want to use it for editing. But I don’t know how to pass values to it, before I combined the tables everything worked fine.
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
{
/// <summary>
/// Логика взаимодействия для CabinetPage.xaml
/// </summary>
public partial class CabinetPage : Page
{
NormApparatYchetEntities2 dataEntetis = new NormApparatYchetEntities2();
public CabinetPage()
{
InitializeComponent();
DGCabinetViev.ItemsSource = NormApparatYchetEntities2.GetContext().CabinetTable.ToList();
var query =
from SpCab in dataEntetis.CabinetTable
orderby SpCab.IdPolzovatel
select new
{
SpCab.IdPolzovatel,
SpCab.NomerCabineta,
SpCab.PolzovatelTable.Name,
SpCab.IdComplect,
};
DGCabinetViev.ItemsSource = query.ToList();
}
private void UpdateCabinetClick(object sender, RoutedEventArgs e)
{
DGCabinetViev.ItemsSource = NormApparatYchetEntities2.GetContext().CabinetTable.ToList();
var query =
from SpCab in dataEntetis.CabinetTable
orderby SpCab.IdPolzovatel
select new
{
SpCab.IdPolzovatel,
SpCab.NomerCabineta,
SpCab.PolzovatelTable.Name,
SpCab.IdComplect,
};
DGCabinetViev.ItemsSource = query.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();
}
private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
{
}
}
}
<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 Name}" 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" TextChanged="TextBox_TextChanged"/>
<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 questionAsk a Question
731 491 924 answers to any question