Frank Dzaebel, erstellt am: 7.8.2006, zuletzt geändert:
23.10.2010
Kategorie: Datenbindung, .NET-Version: 2.0, [Download], [Download2],
[Download3], MSDN-Beispiel
In MSDN-Beispielen werden oft DataPropertyName's über String's gesetzt.
Dies ist möglich, jedoch kann man das auch "typsicherer" erledigen.
Der nachfolgende Code bietet nur ein Beispiel möglicher Implementationen.
Im Download2 befindet sich ein Beispiel mit einer DataGridViewComboBoxColumn über
eine Nachschlagespalte mit zwei Tabellen über eine Access-Datenbank.
Für weiterführende Beispiele mit generischen Datenbindungen wie die generische BindingList siehe:
- [Custom Data Binding, Part1, Part2, Part 3]
- [ITypedList-Schnittstelle]
using System;
using System.Windows.Forms;
using System.Data;
using TypsichereProperties;
using System.ComponentModel;
using System.Reflection;
namespace TypsichereProperties
{
using Cols = EnumsAndComboBox.Knight.Columns;
public enum Title {King, Sir};
public partial class EnumsAndComboBox : Form
{
private DataGridView dataGridView1 = new DataGridView();
private BindingSource bindingSource1 = new BindingSource();
public EnumsAndComboBox()
{
this.Load += new System.EventHandler(EnumsAndComboBox_Load);
}
private void EnumsAndComboBox_Load(object sender,System.EventArgs e)
{
// Populate the data source.
bindingSource1.Add(new Knight(Title.King,"Uther" ,true ));
bindingSource1.Add(new Knight(Title.King,"Arthur" ,true ));
bindingSource1.Add(new Knight(Title.Sir ,"Mordred",false));
bindingSource1.Add(new Knight(Title.Sir ,"Gawain" ,true ));
bindingSource1.Add(new Knight(Title.Sir ,"Galahad",true ));
// Initialize the DataGridView.
dataGridView1.AutoGenerateColumns = false;
dataGridView1.AutoSize = true;
dataGridView1.DataSource = bindingSource1;
dataGridView1.Columns.Add(CreateComboBoxWithEnums());
// Initialize and add a text box column.
DataGridViewColumn column = new DataGridViewTextBoxColumn();
column.DataPropertyName = Cols.Name; // <-- Hier ohne geschriebenen String
column.Name = "Knight";
dataGridView1.Columns.Add(column);
// Initialize and add a check box column.
column = new DataGridViewCheckBoxColumn();
column.DataPropertyName = Cols.GoodGuy; // <-- Hier ohne geschriebenen String
column.Name = "Good";
dataGridView1.Columns.Add(column);
// Initialize the form.
this.Controls.Add(dataGridView1);
this.AutoSize = true;
this.Text = "Objektbindung ohne Strings";
}
DataGridViewComboBoxColumn CreateComboBoxWithEnums()
{
DataGridViewComboBoxColumn combo = new DataGridViewComboBoxColumn();
combo.DataSource = Enum.GetValues(typeof(Title));
combo.DataPropertyName = Cols.Title; // <-- Hier ohne geschriebenen String
combo.Name = "MeinTitel";
return combo;
}
#region "business object"
public class Knight
{
private string hisName;
private bool good;
private Title hisTitle;
public Knight(Title title,string name,bool good)
{
hisTitle = title;
hisName = name;
this.good = good;
}
public Knight()
{
hisTitle = Title.Sir;
hisName = "<enter name>";
good = true;
}
public string Name
{
get{return hisName;}
set{hisName = value;}
}
public bool GoodGuy
{
get{return good;}
set{good = value;}
}
public Title Title
{
get{return hisTitle;}
set{hisTitle = value;}
}
static public class Columns
{
static Columns()
{
FieldInfo[] fls = typeof(Columns).GetFields();
foreach (FieldInfo fi in fls)
fi.SetValue(fi,fi.Name);
}
public static string Name;
public static string GoodGuy;
public static string Title;
}
}
#endregion
}
}