2010-06-19 5 views

Répondre

0

Je pense que vous êtes à la recherche au mauvais endroit. Pour l'événement de changement de valeur, vous devez utiliser l'événement modifié par la propriété de votre classe qui est lié à cette colonne spécifique dans GridView. Parce que, lorsque vous modifiez la valeur dans le ComboBoxColumn, il va à son tour mettre à jour la valeur dans l'objet qui est lié à cette ligne.

Exemple:

Designer Code généré

`classe partielle Form1 {/// /// variable concepteur requis. /// private System.ComponentModel.IContainer components = null;

/// <summary> 
    /// Clean up any resources being used. 
    /// </summary> 
    /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param> 
    protected override void Dispose(bool disposing) 
    { 
     if (disposing && (components != null)) 
     { 
      components.Dispose(); 
     } 
     base.Dispose(disposing); 
    } 

    #region Windows Form Designer generated code 

    /// <summary> 
    /// Required method for Designer support - do not modify 
    /// the contents of this method with the code editor. 
    /// </summary> 
    private void InitializeComponent() 
    { 
     this.dataGridView1 = new System.Windows.Forms.DataGridView(); 
     this.Column1 = new System.Windows.Forms.DataGridViewComboBoxColumn(); 
     this.Column2 = new System.Windows.Forms.DataGridViewTextBoxColumn(); 
     ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit(); 
     this.SuspendLayout(); 
     // 
     // dataGridView1 
     // 
     this.dataGridView1.AllowUserToAddRows = false; 
     this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize; 
     this.dataGridView1.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] { 
     this.Column1, 
     this.Column2}); 
     this.dataGridView1.Dock = System.Windows.Forms.DockStyle.Fill; 
     this.dataGridView1.Location = new System.Drawing.Point(0, 0); 
     this.dataGridView1.Name = "dataGridView1"; 
     this.dataGridView1.RowTemplate.Height = 24; 
     this.dataGridView1.Size = new System.Drawing.Size(282, 255); 
     this.dataGridView1.TabIndex = 0; 
     // 
     // Column1 
     // 
     this.Column1.DataPropertyName = "Value1"; 
     this.Column1.HeaderText = "Column1"; 
     this.Column1.Name = "Column1"; 
     // 
     // Column2 
     // 
     this.Column2.DataPropertyName = "Value2"; 
     this.Column2.HeaderText = "Column2"; 
     this.Column2.Name = "Column2"; 
     // 
     // Form1 
     // 
     this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 16F); 
     this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; 
     this.ClientSize = new System.Drawing.Size(282, 255); 
     this.Controls.Add(this.dataGridView1); 
     this.Name = "Form1"; 
     this.Text = "Form1"; 
     this.Load += new System.EventHandler(this.Form1_Load); 
     ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit(); 
     this.ResumeLayout(false); 

    } 

    #endregion 

    private System.Windows.Forms.DataGridView dataGridView1; 
    private System.Windows.Forms.DataGridViewComboBoxColumn Column1; 
    private System.Windows.Forms.DataGridViewTextBoxColumn Column2; 
}` 

code Derrière

`public partial class Form1: Formulaire { publique Form1() { InitializeComponent(); }

private void Form1_Load(object sender, EventArgs e) 
    { 
     // Column1 is 
     Column1.DataPropertyName = "Value1"; 
     Column1.Items.AddRange(Enumerable.Range(1, 10).Select(i => i.ToString()).ToArray()); 
     Column2.DataPropertyName = "Value2"; 
     dataGridView1.DataError += new DataGridViewDataErrorEventHandler(dataGridView1_DataError); 
     dataGridView1.DataSource = (from obj in Enumerable.Range(1, 20) 
            select new Model() { Value1 = obj, Value2 = ("Value2 #" + obj.ToString()) }).ToList(); 
    } 

    void dataGridView1_DataError(object sender, DataGridViewDataErrorEventArgs e) 
    { 
     System.Diagnostics.Debug.WriteLine(e.Exception.ToString()); 
    } 
} 

class Model 
{ 
    protected Int32 _value1; 
    public Int32 Value1 
    { 
     get 
     { 
      return _value1; 
     } 
     set 
     { 
      // Here is your property change event 
      MessageBox.Show(value.ToString()); 
      _value1 = value; 
     } 
    } 

    public String Value2 { get; set; } 
}` 
Questions connexes