Click here to Skip to main content
15,900,685 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I want to Bind ComboBoxColumn in DataGridView, not to Add it into DataGridView, means I have a ComboBox in DataGridView. Only I have to display data in it.

In Web I used FindControl, but in Windows how can I Get ComboBox Control

Also how do I Fire Event in ComboBox of DataGridView.
Posted
Updated 12-Apr-13 2:55am
v2

C#
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e) {

        if( dataGridView1.Rows[e.RowIndex].Cells["Combobox"].Value <>"")
         {
            //Do what u want
          }

       }
 
Share this answer
 
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace WinDemo
{
    public partial class Form4 : Form
    {
        SqlConnection con = new SqlConnection("Server=.;DataBase=hospitalA;UID=sa;Pwd=sa;");
        DataGridViewComboBoxColumn cmbI;
        ComboBox cmb1; TextBox txtQ; string IN, IC, IR, IQ;
        public Form4()
        {
            InitializeComponent();
            cmbI = new DataGridViewComboBoxColumn();
            DataGridViewTextBoxColumn txt1 = new DataGridViewTextBoxColumn();
            DataGridViewTextBoxColumn txt2 = new DataGridViewTextBoxColumn();
            DataGridViewTextBoxColumn txt3 = new DataGridViewTextBoxColumn();
            DataGridViewTextBoxColumn txt4 = new DataGridViewTextBoxColumn();
            DataGridViewTextBoxColumn txt5 = new DataGridViewTextBoxColumn();
            dataGridView1.Columns.Add(cmbI);
            dataGridView1.Columns.Add(txt1);
            dataGridView1.Columns.Add(txt2);
            dataGridView1.Columns.Add(txt3);
            dataGridView1.Columns.Add(txt4);
            dataGridView1.Columns.Add(txt5);
            FillDDL();
        }
        void FillDDL()
        {
            string sqlId = "Select * From Items;";
            SqlDataAdapter dAdapter = new SqlDataAdapter(sqlId, con);
            DataTable dt = new DataTable();
            dAdapter.Fill(dt);
            cmbI.DataSource = dt;
            cmbI.DisplayMember = "it_name";
            cmbI.ValueMember = "ItemCode";
        }
        private void button1_Click(object sender, EventArgs e)
        {
            Form1 ObjViewer = new Form1();
            this.Hide();
            ObjViewer.ShowDialog();
        }
        private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
        {
            if (dataGridView1.CurrentCell.ColumnIndex == dataGridView1.Columns[0].Index)
            {
                cmb1 = e.Control as ComboBox;
                cmb1.SelectionChangeCommitted -= new EventHandler(comboBox1_SelectedIndexChanged);
                cmb1.SelectionChangeCommitted += new EventHandler(comboBox1_SelectedIndexChanged);
            }
            else if (dataGridView1.CurrentCell.ColumnIndex == dataGridView1.Columns[4].Index)
            {
                txtQ = e.Control as TextBox;
                txtQ.TextChanged -= new EventHandler(textBox1_TextChanged);
                txtQ.TextChanged += new EventHandler(textBox1_TextChanged);
            }
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            GetData();
            dataGridView1.CurrentRow.Cells[1].Value = cmb1.Text;
            dataGridView1.CurrentRow.Cells[2].Value = cmb1.SelectedValue;
            dataGridView1.CurrentRow.Cells[3].Value = IR;

            
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            dataGridView1.CurrentRow.Cells[5].Value = Convert.ToString(Convert.ToDecimal(IR) * Convert.ToDecimal(txtQ.Text));
        }
        void GetData()
        {
            con.Open();
            SqlCommand cmd = new SqlCommand("SELECT * from Items where ItemCode='" + cmb1.SelectedValue + "'", con);

            SqlDataReader dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                //IN = dr["it_name"].ToString();
                IR = dr["sa_price"].ToString();
            }
            con.Close();
        }
    }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900