在DataGridView中使用DataGridViewComboBoxColumn及其值 [英] Using DataGridViewComboBoxColumn and its value in the DataGridView

查看:913
本文介绍了在DataGridView中使用DataGridViewComboBoxColumn及其值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的DataGridView中创建了comboBox列

as

DataGridViewCell ComboCell = new DataGridViewComboBoxCell();

DataGridViewComboBoxColumn comboCol = new DataGridViewComboBoxColumn( );

comboCol.AutoComplete = true;







comboCol。 CellTemplate = ComboCell;

comboCol.DisplayMember =MenuName;

comboCol.ValueMember =MenuID;

comboCol.HeaderText =菜单名称;

comboCol.DataSource = dealList;

comboCol.Name =MenuName;

comboCol.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter ;

comboCol.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;

comboCol.Width = 120;

Grd.Columns.Add(comboCol ); <无线电通信/>




现在我想如果组合框中的索引在此列中更改说MenuName然后我可以将所选值分配给另一列说MenuID

我怎么能这样做?

请指导我...提前谢谢你

I have created comboBox column in my DataGridView
as
DataGridViewCell ComboCell = new DataGridViewComboBoxCell();
DataGridViewComboBoxColumn comboCol = new DataGridViewComboBoxColumn();
comboCol.AutoComplete = true;



comboCol.CellTemplate = ComboCell;
comboCol.DisplayMember = "MenuName";
comboCol.ValueMember = "MenuID";
comboCol.HeaderText = "Menu Name";
comboCol.DataSource = dealList;
comboCol.Name = "MenuName";
comboCol.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
comboCol.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
comboCol.Width = 120;
Grd.Columns.Add(comboCol);


Now i want if the index in the combobox is changed in this column say "MenuName" then i can assign the selected value to another column say "MenuID"
how can i do this?
Please guide me... Thank you in advance

推荐答案

Hello WaseemAmin,



遗憾的是,DataGridViewComboBoxColumn不会公开SelectedIndexChanged事件。为了捕获ComboBoxColumn的索引更改,您首先需要将单元格转换为DataGridViewComboBoxEditingControl并将EventHandler添加到该控件。这将是你必须在DataGridView的EditingControlShowing事件中做的。



假设我有一个名为dataGridViewSales的datagridview。在datagridview中我有一个组合框。现在我想根据组合框选择值更改datagrid视图第二列值。



Hello WaseemAmin,

Unfortunately the DataGridViewComboBoxColumn doesn't expose a SelectedIndexChanged Event. In order to catch an index change of a ComboBoxColumn, you 'll first need to cast the cell to a DataGridViewComboBoxEditingControl and Add an EventHandler to that control. This you will have to do in the EditingControlShowing Event of the DataGridView .

suppose i have a datagridview named as dataGridViewSales .and in the datagridview i have one combobox . Now i want to change the datagrid view second column value according to combobox selected value.

private void dataGridViewSales_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
       {
           ComboBox cb = e.Control as ComboBox;
           if (cb != null)
           {
               cb.SelectedIndexChanged += new EventHandler(selectionchange);
              //here SelectedIndexChanged event is used
           }
       }





然后定义selectionchange方法





and then define selectionchange method

void selectionchange(object sender, EventArgs e)
       {
           try
           {
               ComboBox cb = (ComboBox)sender;
               if (cb.Text == "yes")
               {
                   dataGridViewSales.CurrentRow.Cells[1].Value= "Hello";
//if datagridviewcombobox text is yes then set the first column value to 'Hello'
               }
               else if (cb.Text == "no")
               {
                   dataGridViewSales.CurrentRow.Cells[1].Value = string.Empty;
 //else set the first column  value to empty
               }
           }
           catch { }
       }





谢谢



thanks


这篇关于在DataGridView中使用DataGridViewComboBoxColumn及其值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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