如何在Gridview中获取ComboBox的事件

时间:2010-06-19 12:13:08

标签: c#

如何使用C#Windows应用程序在Gridview内部获取ComboBox事件...任何人告诉我这个问题的解决方案.....

提前致谢...

2 个答案:

答案 0 :(得分:0)

答案 1 :(得分:0)

我认为你正在寻找错误的地方。对于值更改事件,您应该使用绑定到GridView中该特定列的类的属性更改事件。因为,当您更改ComboBoxColumn中的值时,它将依次更新绑定到该行的对象中的值。

示例:

Designer生成的代码

`部分类Form1     {         ///         ///所需的设计变量。         ///         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;
}`

背后的代码

`public partial class Form1:Form     {         公共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; }
}`