📜 ⬆️ ⬇️

We use DataGrid in WPF

Not so long ago, I needed to use a table in a C # application. By naivety, I thought that I would use the freshest of everything and chose WPF instead of WinForm for the project and, of course, took the freshest .net framework 4.5. This is where the problems started. As always, I decided that everything would be found in Yandex, but it was not there - on the Internet (or rather, in RuNet) there is nothing at all about the DataGrid. Constantly, no matter how tricky I changed requests, I got on the DataGridView from WinForm. The most interesting thing is that the WPF platform has been completely redesigned and much of what worked in WinForm in WPF does not work at all.

It is also interesting that WPF should be easier to use, because Microsoft separated designers from programmers, added a vector rendering system, XAML, and so on.

After prolonged resentment, I opened a large and fat book on WPF. And now, a miracle! Everything was there, how to resize, rename and repaint anything and anything, but there was no word about how to get an entry from the DataGrid.
')
Desperate, I decided to switch to WinForm, but remembering the good old proverb “if the program does not work, then the problem is not in the code, but in the programmer”, I decided to understand the problem and (I don’t remember on which site) found a solution (it was in English in the darkest depths of the Internet) . The name of the site remembered the link at the end of the document.

I give an example of how to put and retrieve information from a DataGrid in WPF:


To begin with, we will create a new application and place the DataGrid element in the main window, call it grid.
Next, create a class that will store information for the table:

class MyTable { public MyTable(int Id, string Vocalist, string Album, int Year) { this.Id = Id; this.Vocalist = Vocalist; this.Album = Album; this.Year = Year; } public int Id { get; set; } public string Vocalist { get; set; } public string Album { get; set; } public int Year { get; set; } } 


After that, fill the table using the Loaded event:

 //    private void grid_Loaded(object sender, RoutedEventArgs e) { List<MyTable> result = new List<MyTable>(3); result.Add(new MyTable(1, " ", "Thriller", 1982)); result.Add(new MyTable(2, "AC/DC", "Back in Black", 1980)); result.Add(new MyTable(3, "Bee Gees", "Saturday Night Fever", 1977)); result.Add(new MyTable(4, "Pink Floyd", "The Dark Side of the Moon", 1973)); grid.ItemsSource = result; } 


After all, we get information about the line for the MouseUp event (click the mouse):

  //    private void grid_MouseUp(object sender, MouseButtonEventArgs e) { MyTable path = grid.SelectedItem as MyTable; MessageBox.Show(" ID: " + path.Id + "\n : " + path.Vocalist + "\n : " + path.Album + "\n : " + path.Year); } 


Conclusion


Please note that accessing table elements in WPF is indeed easier than in WinForm (recall that WinForm requires the column and cell number), moreover, the table retains the original data type and is very convenient when processing them.

Screenshot of what happened:



The full source XAML code of the application (one of the WPF goodies, you can see the type of element, its name and events subscribed to it, as well as other information):
 <Window x:Class="DataGrid.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="350" Width="525"> <Grid> <DataGrid x:Name="grid" HorizontalAlignment="Left" Height="251" Margin="23,23,0,0" VerticalAlignment="Top" Width="455" Loaded="grid_Loaded" MouseUp="grid_MouseUp"/> </Grid> </Window> 


Full source code in C #:
 using System; using System.Collections.Generic; using System.Linq; using System.Text; 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 DataGrid { /// <summary> /// Interaction logic for MainWindow.xaml /// </summary> public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } //   private void grid_Loaded(object sender, RoutedEventArgs e) { List<MyTable> result = new List<MyTable>(3); result.Add(new MyTable(1, " ", "Thriller", 1982)); result.Add(new MyTable(2, "AC/DC", "Back in Black", 1980)); result.Add(new MyTable(3, "Bee Gees", "Saturday Night Fever", 1977)); result.Add(new MyTable(4, "Pink Floyd", "The Dark Side of the Moon", 1973)); grid.ItemsSource = result; } //    private void grid_MouseUp(object sender, MouseButtonEventArgs e) { MyTable path = grid.SelectedItem as MyTable; MessageBox.Show(" ID: " + path.Id + "\n : " + path.Vocalist + "\n : " + path.Album + "\n : " + path.Year); } } class MyTable { public MyTable(int Id, string Vocalist, string Album, int Year) { this.Id = Id; this.Vocalist = Vocalist; this.Album = Album; this.Year = Year; } public int Id { get; set; } public string Vocalist { get; set; } public string Album { get; set; } public int Year { get; set; } } } 


For example, used Visual Studio 2012 and .net 4.0.

[ Project Source Code ]

Source to: www.dotnetperls.com/datagrid

PS I don’t know, maybe I told an obvious thing for someone, but I spent a lot of time on it, so I decided to share it.

Source: https://habr.com/ru/post/270413/


All Articles