Ich habe eben einen Artikel zu den Spracherweiterungen von C# 3.0 erstellt.
Spracherweiterungen in C# 3.0
Spracherweiterungen in C# 3.0

using(NorthwindDataContext ctx = new NorthwindDataContext())
{
var customers = from c in ctx.Customers
where c.City == "London"
select c;
}
using(NorthwindDataContext ctx = new NorthwindDataContext())
{
Customer customer =
ctx.Customers.Single( c => c.CustomerID == "ALFKI" );
customer.City = "New City";
ctx.SubmitChanges();
}
using(NorthwindDataContext ctx = new NorthwindDataContext())
{
Customer customer =
ctx.Customers.Single( c => c.CustomerID == "ALFKI" );
Order order = new Order
{
OrderDate = DateTime.Now,
ShipCountry = "Berlin",
ShippedDate = DateTime.Now.AddMonths(2)
};
customer.Orders.Add( order );
ctx.SubmitChanges();
}
using(NorthwindDataContext ctx = new NorthwindDataContext())
{
Order order =
ctx.Orders.Single( o => o.OrderID == 11080 );
ctx.Orders.Remove( order );
ctx.SubmitChanges();
}
[ToolboxData("<{0}:HighlightedGridView runat=serverAls nächstes überschreiben wir OnRowCreated und setzen die RowHighlightColor.
ID=HighlightedGridView1></{0}:HighlightedGridView>")]
public class HighlightedGridView : GridView
{
[Category("Behavior")]
[DefaultValue("false")]
public bool HighlightRowOnMouseOver
{
get { return ViewState["rHighlight"] == null ?
false : (bool)ViewState["rHighlight"]; }
set { ViewState["rHighlight"] = value; }
}
[Category("Appearance")]
[Description("---RowHighlightColor---")]
public Color RowHighlightColor
{
get { return ViewState["hlColor"] == null ?
Color.White : (Color)ViewState["hlColor"]; }
set { ViewState["hlColor"] = value; }
}
}
protected override void OnRowCreatedNun müssen wir nur noch die Methode erstellen, die sich um das Highlighting kümmert. Ich weiße hier mit BackColor immer die Hintergrundfarbe zu. Dies kann aber zu unerwünschten Effekten führen, wenn eine alternierende RowColor eingestellt wurde. Aber das überlasse ich dem aufmerksamen Leser.
(GridViewRowEventArgs e)
{
base.OnRowCreated(e);
if (this.HighlightRowOnMouseOver &&
e.Row.RowType == DataControlRowType.DataRow)
SetRowHighlightColor(e.Row);
}
private void SetRowHighlightColor(GridViewRow row)
{
row.Attributes.Add("onmouseover",
"this.style.backgroundColor='" +
Utils.ColorToHexString(RowHighlightColor) + "'");
row.Attributes.Add("onmouseout",
"this.style.backgroundColor='" +
Utils.ColorToHexString(BackColor) + "'");
}
public partial class ListViewEx : ListView
{
BindingSource bindingSource;
public ListViewEx()
{
bindingSource = new BindingSource();
this.View = View.Details;
this.FullRowSelect = true;
this.GridLines = true;
bindingSource.ListChanged += OnListChanged;
}
}
[AttributeProvider(typeof(IListSource))]
public object DataSource
{
get { return bindingSource.DataSource; }
set { bindingSource.DataSource = value; }
}
[Editor("System.Windows.Forms.Design.DataMemberListEditor,
System.Design, Version=2.0.0.0, Culture=neutral,
PublicKeyToken=b03f5f7f11d50a3a", typeof(UITypeEditor))]
public string DataMember
{
get { return bindingSource.DataMember; }
set { bindingSource.DataMember = value; }
}
void OnListChanged(object sender, ListChangedEventArgs e)
{
this.SetColumnHeaders();
this.SetValues();
}
private void SetColumnHeaders()
{
if (bindingSource.List.Count > 0)
{
this.Columns.Clear();
object dataItem = bindingSource.List[0];
foreach (PropertyDescriptor prop in
TypeDescriptor.GetProperties(dataItem))
{
if(prop.PropertyType.GetInterface("IList")
== null)
{
if (prop.ComponentType == typeof(string))
this.Columns.Add("Value");
else
this.Columns.Add(prop.DisplayName);
}
}
}
}
private void SetValues()
{
this.Items.Clear();
foreach (object dataItem in bindingSource.List)
{
if (dataItem is string)
this.Items.Add((string)dataItem);
else if
(TypeDescriptor.GetProperties(dataItem).Count==0
&& dataItem is System.ValueType)
{
if (this.Columns.Count == 0)
this.Columns.Add("Value");
this.Items.Add(dataItem.ToString());
}
else
{
PropertyDescriptorCollection props =
TypeDescriptor.GetProperties(dataItem);
List<string> valueList = new List<string>();
foreach (PropertyDescriptor propDesc in props)
{
string value =
propDesc.GetValue(dataItem) == null ?
string.Empty :
propDesc.GetValue(dataItem).ToString();
valueList.Add(value);
}
this.Items.Add
(new ListViewItem(valueList.ToArray()));
}
}
}
public static class Singleton<T> where T : new()
{
private static T instance = new T();
public static T Instance
{
get { return instance; }
}
}