WPF UND DATENBANK
Database problems and solutions in WPF

Keywords Description Beschreibung Solution
select command How to use a select command Wie rufe ich mit einem Select-Befehl Daten aus der Datenbank? public DataTable selectDBS(string sSQL) {
try {
FbDataAdapter da = new FbDataAdapter(sSQL, _cnn);
DataTable dt = new DataTable("RELATIONS");
da.Fill(dt);
return dt;
}
catch (Exception e) {
MessageBox.Show("Fehlerhafte SQL-Anweisung\r\n" + e.Message + "\r\n" + sSQL, "SQL-Fehler", MessageBoxButton.OK, MessageBoxImage.Error);
return null;
}
}
dbs.txt
wrapper class how can I create a wrapper class ? wie kann ich die DataTable in eine Klasse umwandeln? 1) You need a class "Department" with an interface "INotifyPropertyChanged"
2) each attrib must send an Change-Event
3) you need an array to save the objects (ObservableCollection<Department>)
4) Now, you only need to set the ItemsSource from a datagrid
5) datagrid.ItemsSource = dept_observerlist
navigator in WPF? how can I use a navigatorbar? wie kann ich einen Navigator-Leiste einfügen? First Solution: a navigatorbar in each form
1) create a grid
2) insert the buttons with images
3) create the click-events

Second Solution: a navigatorbar as Usercontrols
1) you create an usercontrol
2) insert the buttons with images
3) create the click-events in the usercontrols
4) create an interface "INavigatorbar"
5) the constructor get the parent "INavigatorbar"
6) in the click-events call the methods from "INavigatorbar"

navigatorbar_xml.txt
navigatorbar_cs.txt
navigatorbar_interface.txt
problems with the navigatorbar event how can I scoll the datagrid rows? wie kann ich eine Zeile in einem Datagrid ansteuern? 1) Change the selectedIndex
2) scroll to View
3) Focus setzen

Prev-Event:
datagrid.SelectedIndex = datagrid.SelectedIndex - 1;
datagrid.ScrollIntoView(datagrid.SelectedItem);
datagrid.Focus();
navigatorbar_datagrid.txt
ObservableCollection with delete items, datagrid how can I "save" the delete-Object and recognize the new and modify Objects to modify the database? wie kann ich die gelöschten Objekte mit merken und die neuen und geänderten Objekte erkennen?
Dann kann ich damit die Datenbank ändern.
1) ModelObject has two new attribs
1a) bool newAttrib=true
1b) bool modifyAttrib=false

2) in the wrapper-Class (dataTable -> ObservableCollection ) change all attribt to false
2a) newAttrib=false
2b) modifyAttrib=false

3) in each setter-method set the modify-attrib

4) Now each new object has set the newAttrib

5) save the delete-object in a separate ObservableCollection

6) ask the user to save the changes (perhaps ;-)

7) save methods
7a) delete the items in the delete-list
7b) insert the new items
7c) update the modify items, that are not new

datagrid_new_delete_modify.txt
datagrid create manuel columns how can I create manuel the columns from an class? wie kann ich manuell die Spalten in einem Datagrid erzeugen Type myType = typeof(ModelEmployee);
PropertyInfo[] propertyInfos = myType.GetProperties();
foreach (PropertyInfo propertyInfo in propertyInfos) {
System.Diagnostics.Debug.WriteLine(propertyInfo.Name);
DataGridTextColumn dataColumn = new DataGridTextColumn();
dataColumn.Header = propertyInfo.Name;
Binding binding= new Binding(propertyInfo.Name);
binding.Mode = BindingMode.TwoWay;
binding.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
dataColumn.Binding = binding;
datagrid.Columns.Add(dataColumn);
}
datagrid_manual_column.txt
sort in a datagrid How can I sort two columns in a datagrid? Wie kann ich zwei Spalten automatisch sortieren? ICollectionView collView = null;
datagrid.ItemsSource = observerItems;
collView = CollectionViewSource.GetDefaultView(datagrid.ItemsSource);
collView.SortDescriptions.Add(new SortDescription("Lastname", ListSortDirection.Ascending));
collView.SortDescriptions.Add(new SortDescription("Firstname", ListSortDirection.Ascending));

datagrid_sort_column.txt
datagrid computes by column I have a table with a "computes by" column.
Samples:
length=to-from
yearsalary=monthsalary*12
name=Lastname+', '+firstname

How can I updated the "computes by column"?
Ich habe eine Entity-tabelle mit einer Spalte, die automatisch berechnet wird.
In Firebird nennt man das "computes by"

Beispiele:
length=to-from
yearsalary=monthsalary*12
name=Lastname+', '+firstname

Wie kann man diese Spalte automatisch updated?
nach dem Speichern und neuladen ist der Wert ja da, doch während des Editierens?
The problem ist, that the Change-Event is fired with the previous-Value, so without any code, you see the last change.

Solution:
You must implemented a separate method to fire an update-Event

datagrid_computed_by.txt

// After the cell lost the focus, the yearsalary ist set to the new value
private void datagrid_CellEditEnding(...) {
if (col.Header.Equals("Monthsalary")) {
DataGridRow row = e.Row;
ModelEmployee empl = (ModelEmployee)row.Item;
empl.updateYearsalary(); // important
}

In the class ModelEmployee:
public void updateYearsalary() {
NotifyPropertyChanged("Yearsalary");
System.Diagnostics.Debug.WriteLine("updateYearsalary: " + yearsalary);
}
datagrid with separate textbox I want a datagrid with an additional textbox.
How ?
Wie kann ich neben den Datagrid ein und mehrere Textbox anzeigen lassen? The textbox need two values:
1) the instance from the array
2) the name of the attrib

Solution:
you create a grid or any other layout with a textbox.
the layoutcontainer get the instance with the datacontext.
the textbox get the value swith the binding-option.

XAML:
<grid Name="gridDetails">
<TextBox Name="tFirstname" Text="{Binding Path=Firstname, UpdateSourceTrigger=PropertyChanged }"
/>
</grid>
CS:
gridDetails.DataContext = empl_items[0];


datagrid_additional_textbox.txt
datagrid with separate checkbox I want a column checkbox in a datagrid. Wie kann ich ein bool'sches Attribut als Checkbox in einem Datagrid darstellen? <DataGridCheckBoxColumn
Header="IsGoodWorker"
Binding="{Binding Path=IsGoodWorker, UpdateSourceTrigger=PropertyChanged}"
/>
datagrid database "AllInOne" samples Can I get an "AllInOne"-Sample"? Gibt es ein "AllInOne"-Beispiel? SURE
Only program: Database_AllInOne.zip
Picture: Database_AllInOne1.png

Projekt: Project_Database_AllInOne.zip

main.xaml: MainWindow.xaml
main.cs: MainWindow.xaml.cs
datagrid database "AllInOne" samples with master/detail Can I get an "AllInOne"-Sample" with two master/details-datagrids? Gibt es ein "AllInOne"-Beispiel mit zwei Master/Detail-Datagrids? SURE
Only program: Database_AllInOne2.zip
Picture: Database_AllInOne2.png

Projekt: Project_Database_AllInOne2.zip

main.xaml: MainWindow.xaml
main.cs: MainWindow.xaml.cs
datagrid database "AllInOne" samples with DataGridComboBoxColumn and DataGridCheckBoxColumn Can I get an "AllInOne"-Sample" with a DataGridComboBoxColumn and a DataGridCheckBoxColumn? Gibt es ein "AllInOne"-Beispiel mit einer DataGridComboBoxColumn und einer DataGridCheckBoxColumn? SURE
Solution with a column-Definition in XAML
Only program: Database_AllInOne3.zip
Picture: Database_AllInOne3.png

Projekt: Project_Database_AllInOne3.zip

main.xaml: MainWindow.xaml
main.cs: MainWindow.xaml.cs
datagrid database "AllInOne" samples with DataGridComboBoxColumn and a DataGridCheckBoxColumn Code behind Can I get an "AllInOne"-Sample" with a DataGridComboBoxColumn and a DataGridCheckBoxColumn ? Gibt es ein "AllInOne"-Beispiel mit einer DataGridComboBoxColumn und einer DataGridCheckBoxColumn ? SURE
Solution with a column-Definition in CS !!!
Only program: Database_AllInOne4.zip
Picture: Database_AllInOne4.png

Projekt: Project_Database_AllInOne4.zip

main.xaml: MainWindow.xaml
main.cs: MainWindow.xaml.cs


DataGridComboBoxColumn

You need five steps to use a DataGridComboBoxColumn:

1) an ObservableCollection with the list of the items for the combobox
ObservableCollection<ModelDept> dept_items = new ObservableCollection<ModelDept>();

2) Insert the items in the ComboBox
dept_items.Add(new ModelDept(1, "FINZ", "Finanzen")); // pindex, shortname, longname

3) Change the attrib in ModelEmployee
Change the attrib in ModelEmployee from DeptIndex, the key, to an complete Object from typ ModelDept
DeptIndex symbolize the Pindex in a dept
OLD: private long aindex;
NEW: private ModelDept dept;
You must scan the list of dept's to search the correct pindex and set the new attrib "dept"

4) Implement Equal (IMPORTANT)
This methode must override, because it is called to select the Combobox item in the datagrid from the start

public override bool Equals(object obj) {
_if (obj is ModelDept) {
__ModelDept dept = (ModelDept)obj;
__return shortName.Equals(dept.shortName);
_}
_else {
__return false; // this code prevents a cast-Error
_}
}

5) Define the ComboBox in XAML
<DataGridComboBoxColumn Header="Department" x:Name="cbDept" SelectedItemBinding="{Binding Dept}" />

in cs:
cbDept.ItemsSource = dept_items;

Projekt: Project_Database_AllInOne3.zip
main.xaml: MainWindow.xaml
main.cs: MainWindow.xaml.cs 5) Define the ComboBox only in CS
Now all columns are generated with cs-code:
// delete the underline ;-)

datagrid.AutoGenerateColumns = false;
Type myType = typeof(ModelEmployee);
PropertyInfo[] propertyInfos = myType.GetProperties();
foreach (PropertyInfo propertyInfo in propertyInfos) {
_System.Diagnostics.Debug.WriteLine(propertyInfo.Name);
_if (propertyInfo.Name.Equals("Dept")) {
__DataGridComboBoxColumn dataCB = new DataGridComboBoxColumn();
__dataCB.Header = "Department";
__Binding binding2 = new Binding("Dept");
__//binding2.Mode = BindingMode.TwoWay; // bringt probleme
__binding2.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
__dataCB.SelectedItemBinding = binding2;
__dataCB.ItemsSource = dept_items;
__datagrid.Columns.Add(dataCB);
_}
else {
__DataGridTextColumn dataColumn = new DataGridTextColumn();
__dataColumn.Header = propertyInfo.Name;
__Binding binding = new Binding(propertyInfo.Name);
__binding.Mode = BindingMode.TwoWay;
__binding.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
__dataColumn.Binding = binding;
__datagrid.Columns.Add(dataColumn);
_}
} // foreachProjekt: Project_Database_AllInOne4.zip
main.xaml: MainWindow.xaml
main.cs: MainWindow.xaml.cs




DB_ABT

Musterlösung aus der Vorlesung, jetzt auch mit Mitarbeiter-Dialog:

DB_ABT.7z

DB_ABT.zip

wpf_abt.DBL

Views:

FDepartment.xaml:

FEmployee.xaml

FEmployee2.xaml (hier mit dynamischen Spalten und einer Combobox)

FMasterDetail.xaml

Jetzt möglichst viel nach Englisch übertragen

Musterlösung des 4. Labors:

CAD_Loesung.7z

Neue Version von MyCommand:

MyCommand.7z

Der Eintrag der Abteilung hatte keinen Namen.

Jetzt kommt eine Fehlermeldung.




Weitere Links

C#

Grundlagen WPF

WPF mit Quellcode




Scripte

Kapitel Script (Eine Folie pro Seite)
(Powerpoint)
Script (Zwei Folien pro Seite)
(Powerpoint)
PDF-Datei (WinWord)
Einleitung Einleitung.pdf Einleitung-2.pdf SdkXamlBrowser.exe
SdkXamlBrowser.zip
Layout Layout.pdf Layout-2.pdf Bild1.jpg
Bild2.jpg
Bild3.jpg

Beispiele in der Vorlesung:
beispiele_vorl.7z

Vorgefertigte Beispiele in der Vorlesung:
uebungen_vorl.7z

Sprache C# Sprache.pdf Sprache-2.pdf  
Dialog dialog.pdf dialog-2.pdf splitter.txt

ueb31.txt
Grafik grafik.pdf grafik-2.pdf  
Office1 office.pdf office-2.pdf  
Threads thread.pdf thread-2.pdf bsp_threads.7z
DataBinding databinding.pdf databinding-2.pdf  
Datenbanken database.pdf database-2.pdf  



Literatur WPF

Windows Presentation Foundation
Thomas Claudius Huber
Galileo Computing
ISBN 978-3-8362-1538-1Pro WPF on C#2010
Matthew MacDonald
Apress-Verlag
ISBN 978-1-4302-7205-2


Window Presentation Foundation
Adam Nathan
SAMS-Verlag
ISBN 0-672-32891-7

Professional entwickeln mit Visual C# 2012
Grundlagen und Profiwissen
Matthias Geirhos
Galileo-Verlag
ISBN 978-3-8362-1954-9


Visual C# 2012
Grundlagen und Profiwissen
Doberanz, Gewinnus

Hanser-Verlag
ISBN 978-3-446-43439-4


WPF und XAML

Rainer Stropek, Karin Huber
entwickler.press
ISBN 978-3-939084-60-0