views:

353

answers:

2

hi,

How to get event for ComboBox inside of Gridview using C# Windows Application... Anyone Tell me the solution of this problem.....

Thanks in Advance...

A: 

Hi,

I think you are looking at the wrong place. For value change event, you should use your class's property changed event that is bound to that specific column in the GridView. Because, when you change value in the ComboBoxColumn, it will in turn update the value in the object that is bound to that row.

Example:

Designer generated code

`partial class Form1 { /// /// Required designer variable. /// 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 Behind

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