"Verschluckte" Exceptions beim DataGrid
Frank Dzaebel, erstellt am: 23.09.2005, zuletzt geändert: 23.09.2005
Kategorie:DataGrid, .NET-Version:1.1, [NG-Posting]

Das DataGrid kann in einigen Situationen Ausnahmen "verschlucken". Das bedeutet, dass der Programmierer nicht die Möglichkeit hat, diese durch einen lokalen oder globalen Exception-Handler abzufangen. Die Lösung ist ein statischer BusinessObject Event, der dann für alle Business-Objekte zur Verfügung steht. Hier ein Beispiel:

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace DataGridException
{
  public class Form1 : System.Windows.Forms.Form
  {
    private System.Windows.Forms.DataGrid dataGridMeasureLines;
    private System.ComponentModel.Container components = null;

    public Form1()
    {
      InitializeComponent();
    }

    protected override void Dispose( bool disposing )
    {
      if( disposing )
      {
        if (components != null) 
        {
          components.Dispose();
        }
      }
      base.Dispose( disposing );
    }

    #region Vom Windows Form-Designer generierter Code
    private void InitializeComponent()
    {
      this.dataGridMeasureLines = new System.Windows.Forms.DataGrid();
      ((System.ComponentModel.ISupportInitialize)(this.dataGridMeasureLines)).BeginInit();
      this.SuspendLayout();

      this.dataGridMeasureLines.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
        | System.Windows.Forms.AnchorStyles.Left) 
        | System.Windows.Forms.AnchorStyles.Right)));
      this.dataGridMeasureLines.DataMember = "";
      this.dataGridMeasureLines.HeaderForeColor = System.Drawing.SystemColors.ControlText;
      this.dataGridMeasureLines.Location = new System.Drawing.Point(19, 60);
      this.dataGridMeasureLines.Name = "dataGridMeasureLines";
      this.dataGridMeasureLines.Size = new System.Drawing.Size(258, 192);
      this.dataGridMeasureLines.TabIndex = 0;
      // 
      // Form1
      // 
      this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
      this.ClientSize = new System.Drawing.Size(292, 266);
      this.Controls.Add(this.dataGridMeasureLines);
      this.Name = "Form1";
      this.Text = "Form1";
      this.Load += new System.EventHandler(this.Form1_Load);
      ((System.ComponentModel.ISupportInitialize)(this.dataGridMeasureLines)).EndInit();
      this.ResumeLayout(false);

    }
    #endregion

    /// <summary> Der Haupteinstiegspunkt für die Anwendung. </summary>
    [STAThread]
    static void Main() 
    {
       Application.Run(new Form1());
    }

    private void Form1_Load(object sender, System.EventArgs e)
    {
      BOObject obj = new BOObject(new float[]{1.2F, 2.3F, 3.4F});
      object b = dataGridMeasureLines.DataBindings.Add("DataSource", obj, 
        "RoofShape.Measurelines");
    }

  }
  
  class BOObject
  {
    public BOObject(float[] floatArr)
    {
      ArrayList measureLines = new ArrayList();
      foreach (float f in floatArr)
        measureLines.Add(new BOMeasureline(f));
      roofShape = new BORoofShape(measureLines);
    }

    private BORoofShape roofShape;
    public BORoofShape RoofShape
    {
      get{return roofShape;}
      set{roofShape = value;}
    }
  }

  class BORoofShape
  {
    public BORoofShape(ArrayList measurelines)
    {
      this.Measurelines = measurelines;
    }

    private ArrayList measurelines;
    //BOMeasureline - Objektliste
    public ArrayList Measurelines
    {
      get{return measurelines;}
      set{measurelines = value;}
    }
  }

  class BOMeasureline
  {
    public BOMeasureline(float length)
    {
      this.length = length;
    }

    private float length;
    public float Length
    {
      get{return length;}
      set
      {
        if (value > 2) throw new Exception("zu gross");
        length = value;
      }
    }
  }
}