Answer the question
In order to leave comments, you need to log in
How to convert selected cell by double click to int?
I am making a database for accounting equipment and I want to implement the following function:
1. A window for adding a set of equipment opens
2. It contains fields in the following way: equipment name / button
3. After the user clicks on the button, the datagrid list opens (in a new window ) (according to which device name the user has poked in front of) and after that the person in this list looks for the device he needs and double-clicks on it.
4. After these steps, the program should determine which field he clicked on and transfer information about the id of the device, which is on this line, to the page for adding a set of equipment.
I did several steps, reached the double-click selection and after clicking I was able to display the received data in the message box, but this has 2 minuses:
1) the user will have to click on the printer ID
2) if he clicks on the adjacent columns of one line, then the data will not be true.
Therefore, the question is how to make it so that after clicking on any field in the line, it determines its id field and passes it to the variable I need.
That's how I did
using System;
using System.Collections.Generic;
using System.ComponentModel;
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.Shapes;
namespace YchetApparatov
{
/// <summary>
/// Логика взаимодействия для PrinterListWindow.xaml
/// </summary>
public partial class PrinterListWindow : Window
{
//Выбранный элемент
public static int SelectPrinterInt;
public PrinterListWindow()
{
InitializeComponent();
DGPrinterViev.ItemsSource = NormApparatYchetEntities2.GetContext().PrinterTable.ToList(); // здесь я выгружаю в datagrid данные из БД, конкретно в этой таблице содержатся принтеры, Id принтера, модель и тд
}
private void DGPrinterViev_MouseDoubleClick(object sender, MouseButtonEventArgs e) // по двойному клику на datagrid событие реализовал так
{
int selectedColumn = DGPrinterViev.CurrentCell.Column.DisplayIndex;
var selectedCell = DGPrinterViev.SelectedCells[selectedColumn];
var cellContent = selectedCell.Column.GetCellContent(selectedCell.Item);
if (cellContent is TextBlock)
{
MessageBox.Show((cellContent as TextBlock).Text); // вывожу данные, на которые нажал
}
}
}
}
<Window x:Class="YchetApparatov.PrinterListWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:YchetApparatov"
mc:Ignorable="d"
Title="PrinterListWindow" Height="720" Width="1280">
<Grid Margin="0,97,0,34">
<Grid.RowDefinitions>
</Grid.RowDefinitions>
<DataGrid x:Name="DGPrinterViev" AutoGenerateColumns="False" IsReadOnly="True" Margin="0,-3,0,126"
ItemsSource="{Binding Source}" SelectedItem="{Binding SelectedRow}" MouseDoubleClick="DGPrinterViev_MouseDoubleClick">
<DataGrid.Columns>
<DataGridTextColumn Header="Модель принтера" Binding="{Binding ModelPrinter}" ></DataGridTextColumn>
<DataGridTextColumn Header="Цвтеной или ЧБ" Binding="{Binding ColorOrBW}" ></DataGridTextColumn>
<DataGridTextColumn Header="Скорость печати в минуту" Binding="{Binding SkorostPechati}" ></DataGridTextColumn>
<DataGridTextColumn Header="Инвентарный номер принтера" Binding="{Binding InventarNomerPrinter}" ></DataGridTextColumn>
<DataGridTextColumn Header="Серийный номер принтера" Binding="{Binding SeriyNomerPrinter}" ></DataGridTextColumn>
<DataGridTextColumn Header="Дата прихода принтера" Binding="{Binding DataPrihodaPrinter}" ></DataGridTextColumn>
<DataGridTextColumn Header="Дата списания принтера" Binding="{Binding DataSpisanPrinter}" ></DataGridTextColumn>
</DataGrid.Columns>
</DataGrid>
<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"/>
</Grid>
</Window>
Answer the question
In order to leave comments, you need to log in
Good afternoon. As I understand it, you need an ID from the DataGrid row. Isn't it easier to get the selected row from your object bound to the DataGrid and get the Id from it after the cast?
that is, it will
var yourObject=DGPrinterViev.SelectedItem as <ваш тип>;
if(yourObject!=null)
{
int id=yourObject.SeriyNomerPrinter;//либо как называется нужное свойство
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question