当在comboboxcolumn中选择一个项目时,在DatagridviewTextbox列中显示数据 [英] Display data in DatagridviewTextbox column when an item selected in comboboxcolumn

查看:46
本文介绍了当在comboboxcolumn中选择一个项目时,在DatagridviewTextbox列中显示数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我有一个带有datagridviewcomboBox列和datagridviewTextbox列的datagridview.
组合框列包含产品代码,当我选择产品代码时,相应的描述应出现在文本框列中.我已经绑​​定了组合框中的数据,但无法在文本框列中显示数据.

请帮助...

解决方案

您可以使用CellValueChanged事件,然后根据组合框中选择的值设置其他单元格值.

嗨Eager2Lern,

我认为这段代码可以为您提供帮助,这是一个包含comboBox列的项目,该项目可更改textBox列的值.

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        ComboBox cbm;
        DataGridViewCell currentCell;
 
        // This DataTable will become a data source for the DataGridView
        DataTable dt = new DataTable();
 
        public Form1()
        {
            InitializeComponent();
        }
 
        private void Form1_Load(object sender, EventArgs e)
        {
            // Create and fill a DataTable as a source of our combobox column
            DataTable typedt = new DataTable();
            typedt.Columns.Add("typeid", typeof(int));
            typedt.Columns.Add("typename");
            typedt.Columns.Add("typedescription");
            typedt.Rows.Add(0, "Typed 0", "description0");
            typedt.Rows.Add(1, "Typed 1", "description1");
            typedt.Rows.Add(2, "Typed 2", "description2");
            typedt.Rows.Add(3, "Typed 3", "description3");
            typedt.Rows.Add(4, "Typed 4", "description4");
            typedt.AcceptChanges();
 
            // Fill the GridView source table
            dt.Columns.Add("id");
            dt.Columns.Add("name");
            dt.Columns.Add("type", typeof(int));
            dt.Columns.Add("typedescription");
            for (int i = 0; i < 50; i++)
                dt.Rows.Add(i.ToString("000"), "name" + i, i % 4);
 
            dt.AcceptChanges();
 
            // Set the data source for the grid
            dataGridView1.DataSource = dt;
 
            // Create new combobox column and set it''s data source
            DataGridViewComboBoxColumn cbc = new DataGridViewComboBoxColumn();
            cbc.DataPropertyName = "type";
            cbc.DisplayMember = "typename";
            cbc.ValueMember = "typeid";
            cbc.DataSource = typedt;
            cbc.HeaderText = "Type";
 
            // Replace the third column of the grid view with the combobox column
            dataGridView1.Columns.RemoveAt(2);
            dataGridView1.Columns.Insert(2, cbc);
 
            // We will handle these events of the DataGridView
            dataGridView1.CellEndEdit += new DataGridViewCellEventHandler(dataGridView1_CellEndEdit);
            dataGridView1.EditingControlShowing += new DataGridViewEditingControlShowingEventHandler(dataGridView1_EditingControlShowing);
        }
 
        void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            if (cbm != null)
            {
                // Here we will remove the subscription for selected index changed
                cbm.SelectedIndexChanged -= new EventHandler(cbm_SelectedIndexChanged);
            }
        }
 
        void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
        {
            // Here try to add subscription for selected index changed event
            if (e.Control is ComboBox)
            {
                cbm = (ComboBox)e.Control;
                if (cbm != null)
                {
                    cbm.SelectedIndexChanged += new EventHandler(cbm_SelectedIndexChanged);
                }
                currentCell = this.dataGridView1.CurrentCell;
            }
        }
 
        void cbm_SelectedIndexChanged(object sender, EventArgs e)
        {
            // Invoke method if the selection changed event occurs
            BeginInvoke(new MethodInvoker(EndEdit));
        }
 
        void EndEdit()
        {
            // Change the content of appropriate cell when selected index changes
            if (cbm != null)
            {
                DataRowView drv = cbm.SelectedItem as DataRowView;
                if (drv != null)
                {
                    this.dataGridView1[currentCell.ColumnIndex + 1, currentCell.RowIndex].Value = drv[2].ToString();
                    this.dataGridView1.EndEdit();
                }
            }
        }
    }
}



希望对您有所帮助,
:)


Hi All,

I have a datagridview with datagridviewcomboBox column and datagridviewTextbox column.
the combobox column contains product code and when i select a product code the corresponding description should appear in textbox column.I have already binded the data in combobox but not able to display data in textbox column.

Please help...

解决方案

You can make use of CellValueChanged event and there, based on the value selected in the combobox, set the other cell value.


Hi Eager2Lern,

I think this code can help you , it is a project containing a comboBox column that change the textBox column value.

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        ComboBox cbm;
        DataGridViewCell currentCell;
 
        // This DataTable will become a data source for the DataGridView
        DataTable dt = new DataTable();
 
        public Form1()
        {
            InitializeComponent();
        }
 
        private void Form1_Load(object sender, EventArgs e)
        {
            // Create and fill a DataTable as a source of our combobox column
            DataTable typedt = new DataTable();
            typedt.Columns.Add("typeid", typeof(int));
            typedt.Columns.Add("typename");
            typedt.Columns.Add("typedescription");
            typedt.Rows.Add(0, "Typed 0", "description0");
            typedt.Rows.Add(1, "Typed 1", "description1");
            typedt.Rows.Add(2, "Typed 2", "description2");
            typedt.Rows.Add(3, "Typed 3", "description3");
            typedt.Rows.Add(4, "Typed 4", "description4");
            typedt.AcceptChanges();
 
            // Fill the GridView source table
            dt.Columns.Add("id");
            dt.Columns.Add("name");
            dt.Columns.Add("type", typeof(int));
            dt.Columns.Add("typedescription");
            for (int i = 0; i < 50; i++)
                dt.Rows.Add(i.ToString("000"), "name" + i, i % 4);
 
            dt.AcceptChanges();
 
            // Set the data source for the grid
            dataGridView1.DataSource = dt;
 
            // Create new combobox column and set it''s data source
            DataGridViewComboBoxColumn cbc = new DataGridViewComboBoxColumn();
            cbc.DataPropertyName = "type";
            cbc.DisplayMember = "typename";
            cbc.ValueMember = "typeid";
            cbc.DataSource = typedt;
            cbc.HeaderText = "Type";
 
            // Replace the third column of the grid view with the combobox column
            dataGridView1.Columns.RemoveAt(2);
            dataGridView1.Columns.Insert(2, cbc);
 
            // We will handle these events of the DataGridView
            dataGridView1.CellEndEdit += new DataGridViewCellEventHandler(dataGridView1_CellEndEdit);
            dataGridView1.EditingControlShowing += new DataGridViewEditingControlShowingEventHandler(dataGridView1_EditingControlShowing);
        }
 
        void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            if (cbm != null)
            {
                // Here we will remove the subscription for selected index changed
                cbm.SelectedIndexChanged -= new EventHandler(cbm_SelectedIndexChanged);
            }
        }
 
        void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
        {
            // Here try to add subscription for selected index changed event
            if (e.Control is ComboBox)
            {
                cbm = (ComboBox)e.Control;
                if (cbm != null)
                {
                    cbm.SelectedIndexChanged += new EventHandler(cbm_SelectedIndexChanged);
                }
                currentCell = this.dataGridView1.CurrentCell;
            }
        }
 
        void cbm_SelectedIndexChanged(object sender, EventArgs e)
        {
            // Invoke method if the selection changed event occurs
            BeginInvoke(new MethodInvoker(EndEdit));
        }
 
        void EndEdit()
        {
            // Change the content of appropriate cell when selected index changes
            if (cbm != null)
            {
                DataRowView drv = cbm.SelectedItem as DataRowView;
                if (drv != null)
                {
                    this.dataGridView1[currentCell.ColumnIndex + 1, currentCell.RowIndex].Value = drv[2].ToString();
                    this.dataGridView1.EndEdit();
                }
            }
        }
    }
}



I hope this help,
:)


这篇关于当在comboboxcolumn中选择一个项目时,在DatagridviewTextbox列中显示数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆