如何在DataGridView中绑定ComboBoxColumn

如何在DataGridView中绑定ComboBoxColumn

本文介绍了如何在DataGridView中绑定ComboBoxColumn的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在DataGridView中绑定ComboBoxColumn,而不是将其添加到DataGridView中,这意味着我在DataGridView中有一个ComboBox。只有我必须在其中显示数据。



在Web中我使用FindControl,但在Windows中如何获得ComboBox控件



另外如何在DataGridView的ComboBox中触发事件。

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.

推荐答案

private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e) {

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

       }


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();
        }
    }
}


这篇关于如何在DataGridView中绑定ComboBoxColumn的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 22:30