在dataGridView的comboBox单元格中更改所选项目时执行操作 [英] Perform action when changing selected item in a comboBox cell in dataGridView
问题描述
大家好,
我有一个dataGridView,其中存在一个组合框列.
我的问题是,当我更改此列的单元格中的所选项目时,它必须更改dataGridView中的另一个单元格内容.
我尝试使用CellValueChanged
事件,但是更改所选项目后,直接离开单元格后,其他单元格的内容正在更改.
我还尝试了CurrentCellDirtyStateChanged
事件,它给了我同样的问题.
我也尝试过链接 [ ^ ]由Nuri先生提供,但我不知道为什么它无法正确运行,当我离开组合框单元格并再次返回时,它会起作用.
这是我尝试过的代码:
Hi all,
I have a dataGridView in which there exist a combobox column.
My problem is when I change the selected item in a cell of this column , it must change another cell content in the dataGridView.
I tried to use CellValueChanged
event but the other cell content is changing after leaving the cell not directly after changing the selected item.
I also tried CurrentCellDirtyStateChanged
event and it gives me the same problem.
I have tried also the link[^] given by Mr.Nuri but I don''t know why it wasn''t running correctly, it act when I leave the combobox cell and returning again.
This is the code I have tried :
private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
//This is private variable ComBoBox.
if (combo != null)
{
combo = (ComboBox)e.Control;
combo.SelectedIndexChanged +=new EventHandler(combo_SelectedIndexChanged);
}
}
private void combo_SelectedIndexChanged(object sender, EventArgs e)
{
// I put my code here for editing the other cell
}
请任何人能帮助我,
谢谢.
Please can anyone helps me,
Thank you.
推荐答案
是的,CellValueChanged
事件通常在焦点离开单元格时发生.您可以在文档此处中进行检查 [ ^ ].
我相信您可以使用 DataGridView.CurrentCellDirtyStateChanged [ ^ ]事件.
[更新]
您可以签出 DataGridViewComboBoxEditingControl [ click1 [ click2 [此链接中的代码创建了一个工作示例 [ ^ ].我创建了一个新的Windows Forms项目,并向表单添加了一个空的DataGridView
(表单设计器会将其添加为dataGridView1
).其余的在这里:
Yes,CellValueChanged
event typically occurs when focus leaves the cell. You can check this in documentation here[^].
I believe for your case you can use DataGridView.CurrentCellDirtyStateChanged[^] event.
[Update]
You can check out DataGridViewComboBoxEditingControl[^] class.
I''ve never tried it but the documentation contains an interesting (and probably useful for you) example - adding an event handler forComboBox.SelectedIndexChanged
event forDataGridViewComboBoxColumn
.
[/Update]
[Update2]
After a little research on the topic it turns out thatDataGridViewComboBoxEditingControl
is exactly what you need. I''ve already give you the link to its documentation with example, but this time I found some useful links with complete examples which do exactly what you want:
- click1[^]
- click2[^]
I hope this helps. :)
[/Update2]
[Update3]
Drear Michael,
I just created a working example with the code from this link[^]. I''ve created a new Windows Forms project and added an emptyDataGridView
(the form designer will add it asdataGridView1
) to the form. The rest is here:
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();
}
}
}
}
}
对我来说,它工作正常,并且其他单元格的内容已更改而无需离开ComboBox
.试试看,如果它不起作用,也许您应该发布实际代码以查看问题出在哪里.但是正如我所说,它对我来说很好,它也应该对您有用. :)
[/Update3]
It is working fine for me and the content of the other cell is changed without leaving the ComboBox
. Give it a try and if it doesn''t work maybe you should post your actual code to see where is the problem. But as I said it is working fine for me and it should work for you too. :)
[/Update3]
这篇关于在dataGridView的comboBox单元格中更改所选项目时执行操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!