Wednesday, October 31, 2012

Bind combobox in datagridview in editable mode in winform

 I want editable grid in my project, I  do this as below using"  dataGridView1_EditingControlShowing" event.







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

namespace DataGridViewComboBoxBinding
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            this.dataGridView1.AutoGenerateColumns = false;
            this.ColumnPersonTypeCode.DataPropertyName = "Value";
            this.ColumnPersonTypeCode.DisplayMember = "Name";
            this.ColumnPersonTypeCode.ValueMember = "Value";
            this.ColumnPersonTypeCode.DataSource = Findingproduct();
            //this.dataGridView1.DataSource = Findingproduct();
            //Findingproduct();
        private DataTable Findingproduct()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("Name");
            dt.Columns.Add("Value");
            DataRow dr = dt.NewRow();
            dr[0] = "sanjay";
            dr[1] = "1";
            dt.Rows.Add(dr);

            dr = dt.NewRow();
            dr[0] = "Raju";
            dr[1] = "2";
            dt.Rows.Add(dr);

            return dt;
        }

        void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
        {
            if (this.dataGridView1.CurrentCell.ColumnIndex == this.ColumnPersonTypeCode.Index)
            {
              //BindingSource bindingSource = this.dataGridView1.DataSource as BindingSource;
        }
              // Person person = bindingSource.Current as Person;
              //BindingList<PersonType> bindingList = this.FindPersonTypes(person);

                DataGridViewComboBoxEditingControl comboBox = e.Control as DataGridViewComboBoxEditingControl;
                DataGridViewTextBoxEditingControl txt=e.Control as DataGridViewTextBoxEditingControl;
                //comboBox.DataSource = Findingproduct();

                comboBox.SelectionChangeCommitted -= this.comboBox_SelectionChangeCommitted;
                comboBox.SelectionChangeCommitted += this.comboBox_SelectionChangeCommitted;
            }
        }

        void comboBox_SelectionChangeCommitted(object sender, EventArgs e)
        {
            var currentcell = dataGridView1.CurrentCellAddress;
            var sendingCB = sender as DataGridViewComboBoxEditingControl;
            DataGridViewTextBoxCell cel = (DataGridViewTextBoxCell)dataGridView1.Rows[currentcell.Y].Cells[0];
            cel.Value = sendingCB.EditingControlFormattedValue.ToString();

            this.dataGridView1.EndEdit();
        }

       
    }

  
}

After Run Result  window:





Label :winform ,c#,asp.net,combobox,datagridview

If you are searching life partner. your searching end with kpmarriage.com. now kpmarriage.com offer free matrimonial website which offer free message, free chat, free view contact information. so register here : kpmarriage.com- Free matrimonial website

0 comments:

Post a Comment