2019년 1월 26일 토요일

WPF 데이터 그리드(DataGrid)

WPF 데이터 그리드(DataGrid)
Grid는 컨트롤들을 담지만 DataGrid는 사용자 정의 가능한 표 형태로 데이터를 표시하는 컨트롤로 행 및 열에 데이터 or 그 모임을 표시하는 유연한 방법을 제공한다.
DataGrid는 일반적으로 정렬 및 편집 기능을 제공하는 표 형식의 데이터를 사용자에게 표시하기 위한 사용자 인터페이스 구성 요소로 ASP.NET (GridView) 및 Windows Forms (DataGridView)와 유사하다.
ListView 컨트롤의 다양한 기능을 통해 매우 유연한 읽기 전용 DataGrid 기능을 사용할 수 있다.
[실습예제1]간단한 DataGrid
[MainWindow.xaml.xml]
<Window x:Class="WpfApplication5.MainWindow"
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:WpfApplication5"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid Margin="10">
<DataGrid Name="dgUsers" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTextColumn Header="Name" Binding="{Binding Name}" />
<DataGridTemplateColumn Header="Birthday">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<DatePicker SelectedDate="{Binding Birthday}" BorderThickness="0" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
</Grid>
</Window>
[MainWindow.xaml.cs]
using System;
using System.Collections.Generic;
using System.Windows;
namespace WpfApplication5
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
List<User> users = new List<User>();
users.Add(new User() { Id = 1, Name = "홍길동", Birthday = new DateTime(1971, 7, 23) });
users.Add(new User() { Id = 2, Name = "박길동", Birthday = new DateTime(1974, 1, 17) });
users.Add(new User() { Id = 3, Name = "정길동", Birthday = new DateTime(1991, 9, 2) });
dgUsers.ItemsSource = users;
}
}
public class User
{
public int Id { get; set; }
public string Name { get; set; }
public DateTime Birthday { get; set; }
}
}
[실습예제2]DataGrid
[MainWindow.xaml.xml]
<Window x:Class="WpfApplication6.MainWindow"
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:WpfApplication6"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid>
<DataGrid
HorizontalAlignment="Left"
Margin="10,10,0,0"
VerticalAlignment="Top"
Loaded="DataGrid_Loaded"/>
</Grid>
</Window>
[MainWindow.xaml.cs]
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
class Dog
{
public string Name { get; set; }
public int Size { get; set; }
public string Color { get; set; }
public Dog(string name, int size, string color)
{
this.Name = name;
this.Size = size;
this.Color = color;
}
}
namespace WpfApplication6
{
public partial class MainWindow : Window
{
private void DataGrid_Loaded(object sender, RoutedEventArgs e)
{
// ... Create a List of objects.
var items = new List<Dog>();
items.Add(new Dog("Fido", 10, "White"));
items.Add(new Dog("Spark", 20, "Black"));
items.Add(new Dog("Fluffy", 4, "Yellow"));
// ... Assign ItemsSource of DataGrid.
var grid = sender as DataGrid;
grid.ItemsSource = items;
}
}
}
[실습예제3]Filtered DataGrid
[MainWindow.xaml.xml]
<Window x:Class="WpfApplication6.MainWindow"
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"
Title="MainWindow" Height="447.91" Width="514.871" Loaded="Window_Loaded">
<Grid Margin="0,0,2,81">
<DataGrid x:Name="dg"
HorizontalAlignment="Left"
VerticalAlignment="Top"
Height="413" Width="507"
AutoGenerateColumns="False"
Margin="0,0,-2,-76">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Id}"
Header="Id">
<DataGridTextColumn.HeaderTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Content, RelativeSource=
{RelativeSource Mode=TemplatedParent}}"/>
<TextBox x:Name="txtId" Width="100" TextChanged="txtName_TextChanged" />
</StackPanel>
</DataTemplate>
</DataGridTextColumn.HeaderTemplate>
</DataGridTextColumn>
<DataGridTextColumn Binding="{Binding Name}"
Header="Name">
<DataGridTextColumn.HeaderTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Content, RelativeSource=
{RelativeSource Mode=TemplatedParent}}"/>
<TextBox x:Name="txtName" Width="100" TextChanged="txtName_TextChanged" />
</StackPanel>
</DataTemplate>
</DataGridTextColumn.HeaderTemplate>
</DataGridTextColumn>
</DataGrid.Columns>
</DataGrid>
</Grid>
</Window>
[MainWindow.xaml.cs]
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Windows;
using System.Windows.Controls;
using System.ComponentModel;
using System.Windows.Data;
namespace WpfApplication6
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
///
public class Skill
{
public int Years { get; set; }
public string Name { get; set; }
}
public class Person
{
public int Id { get; set; }
public string Name { get; set; }
public string Position { get; set; }
public List<Skill> SkillList { get; set; }
}
public class Persons : ObservableCollection<Person>
{
public Persons()
{
Add(new Person() { Id = 1, Name = "Person1", Position = "Manager", SkillList = new List<Skill>() });
Add(new Person() { Id = 2, Name = "Programmer2", Position = "Programmer", SkillList = new List<Skill>() });
Add(new Person() { Id = 3, Name = "Person3", Position = "Programmer", SkillList = new List<Skill>() });
Add(new Person() { Id = 4, Name = "Person4", Position = "Admin", SkillList = new List<Skill>() });
Add(new Person() { Id = 5, Name = "Person5", Position = "Tester", SkillList = new List<Skill>() });
}
}
public partial class MainWindow : Window
{
private void Window_Loaded(object sender, RoutedEventArgs e)
{
Persons pList = new Persons();
dg.ItemsSource = pList;
}
private void txtName_TextChanged(object sender, TextChangedEventArgs e)
{
TextBox t = (TextBox)sender;
string filter = t.Text;
ICollectionView cv = CollectionViewSource.GetDefaultView(dg.ItemsSource);
if (filter == "")
cv.Filter = null;
else
{
cv.Filter = o =>
{
Person p = o as Person;
if (t.Name == "txtId")
return (p.Id == Convert.ToInt32(filter));
return (p.Name.ToUpper().StartsWith(filter.ToUpper()));
};
}
}
}
}
[실습예제4]Master Detail DataGrid
[MainWindow.xaml.xml]
<Window x:Class="WpfApplication6.MainWindow"
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"
Title="MainWindow" Height="350" Width="400" Loaded="Window_Loaded">
<Grid Margin="0,0,2,81">
<DataGrid x:Name="dg" HorizontalAlignment="Left" VerticalAlignment="Top" Height="196" Width="507" SelectionChanged="dg_SelectionChanged"/>
<DataGrid x:Name="dg1" HorizontalAlignment="Left" VerticalAlignment="Top" Height="214" Width="505" Margin="0,201,0,-78"/>
</Grid>
</Window>
[MainWindow.xaml.cs]
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Windows;
using System.Windows.Controls;
namespace WpfApplication6
{
public class Skill
{
public int Years { get; set; }
public string Name { get; set; }
}
public class Person
{
public int Id { get; set; }
public string Name { get; set; }
public string Position { get; set; }
public List<Skill> SkillList { get; set; }
}
public class Persons : ObservableCollection<Person>
{
public Persons()
{
Add(new Person() { Id = 1, Name = "홍길동", Position = "Programmer", SkillList = new List<Skill>() });
Add(new Person() { Id = 2, Name = "정길동", Position = "Programmer", SkillList = new List<Skill>() });
}
}
public partial class MainWindow : Window
{
private void Window_Loaded(object sender, RoutedEventArgs e)
{
Persons pList = new Persons();
pList[0].SkillList.Add(new Skill() { Years = 5, Name = "C++" });
pList[0].SkillList.Add(new Skill() { Years = 15, Name = "Visual Basic" });
pList[1].SkillList.Add(new Skill() { Years = 7, Name = "C#" });
pList[1].SkillList.Add(new Skill() { Years = 4, Name = "VB.NET" });
dg.ItemsSource = pList;
}
private void dg_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (e.AddedItems[0] is Person) dg1.ItemsSource = ((Person)e.AddedItems[0]).SkillList;
}
}

댓글 없음:

댓글 쓰기

(C#교육동영상)C# ADO.NET 실습 ODP.NET/ODAC 설치 오라클 함수 호출 실습, C#학원, WPF학원, 닷넷학원, 자바학원

  (C#교육동영상)C# ADO.NET 실습  ODP.NET/ODAC 설치  오라클 함수 호출 실습, C#학원, WPF학원, 닷넷학원, 자바학원 https://www.youtube.com/watch?v=qIPU85yAlzc&list=PLxU-i...