C#Datagridview与DatagridComboBoxColumn [英] c# Datagridview with DatagridComboBoxColumn
问题描述
在form1上,我有datagridview,在代码中我添加了列类型DataGridComboboxColumn和接下来的3列带有普通文本的内容.
我像这样填充组合框:
Hi,
On form1 I have datagridview, in code i add column type DataGridComboboxColumn and next 3 columns with normal text.
Combobox i fill like this:
foreach (DataRow row in ds.Tables["tbluslugi"].Rows)
{
nazwa.Items.Add(row["Nazwa"].ToString());
}
我使用事件dataGridView1_CurrentCellDirtyStateChanged
,当我在组合框中选择某个值后,此事件就是启动.但是,当我想找回所选择的价值时,value = false.值=当我转到下一行时的选定值,在这种情况下,我必须使用事件CellEndEdit,但是当我从组合框中选择某物而没有任何反应时,它看起来很糟糕.
我想要的是?我想要当我选择valuse形式组合框时(不要等待下一行),它正在数据库中查找此值,取回数据并将其放在此行的空单元格中.
我还有一个问题,如何在datagridcombobox中设置默认项?
I use event dataGridView1_CurrentCellDirtyStateChanged
, after when i choose in combobox some value this event is Launch. But when i want get back value what was chosen, value = false. Value = chosen value when i go to next row and i this case i have to use event CellEndEdit but it`s look bad, when i choose somthing from combobox and nothing happend.
What i want? I want when i choose valuse form combobox (don`t wait to go next row) it`s looking in database this value, get back data and put it in empty cells in this row.
I have another quastion, how to make default item in datagridcombobox ?
推荐答案
我想我知道您希望在选择之后更改文本单元格中的值在组合框中.
如果是这种情况,请使用EditingControlShowing事件来设置组合框更改事件.
像这样
I think I understand that you want the values in the text cells to change after a selection is made in the combo box.
If this is the case, use the EditingControlShowing event to setup the combo boxes change event.
Like this
private void dataGridView_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
if(dataGridView.CurrentCell.ColumnIndex == 1)
{
ComboBox cmb = (ComboBox)e.Control;
cmb.SelectedValueChanged += new EventHandler(cmb_SelectedValueChanged);
}
}
void cmb_SelectedValueChanged(object sender, EventArgs e)
{
//Update your 3 text fields here
}
希望对您有帮助
Hope this helps
这篇关于C#Datagridview与DatagridComboBoxColumn的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!