问题描述
我有一个 DataGridView的
与 DataGridViewComboBoxColumn
这势必会在列表< IBrand> ;
。在这个组合框中专栏中,我允许用户选择现有值,或键入一个新的。当用户选择一个现有值, IsCurrentRowDirty()
正确返回true。当一个值的用户类型,在 IsCurrentRowDirty()
总是返回false时,它显然不应该返回true。
我已经打过电话 DataGridView.CommitEdit()
在 CurrentCellDirtyStateChanged
事件,但不工作
我如何在 DataGridViewComboBoxColumn
该行设置为脏?
$用户输入的值b $ b
相关代码如下:
谢谢,
凯尔
公共无效BindBrands()
{
DataGridViewComboBoxColumn组合框=(DataGridViewComboBoxColumn)dgvReference.Columns [(INT)ReferenceColumnsIndex.Brand]
comboBox.DisplayMember =姓名;
comboBox.ValueMember =自我; //自我收益这个
comboBox.DataSource =品牌;
}
//这使得DataGridViewComboBoxColumn是由用户
私人无效dgvReference_EditingControlShowing(对象发件人,DataGridViewEditingControlShowingEventArgs E)
{$ B编辑$ b DataGridViewComboBoxColumn列= GetColumn< DataGridViewComboBoxColumn>(dgvReference,
(INT)ReferenceColumnsIndex.Brand);
如果(列!= NULL)
{
组合框下拉框中= e.Control作为组合框;
如果(组合框!= NULL)
{
comboBox.DropDownStyle = ComboBoxStyle.DropDown;
}
}
}
私人无效dgvReference_CellValidating(对象发件人,DataGridViewCellValidatingEventArgs E)
{
开关(e.ColumnIndex)
{
的情况下(INT)ReferenceColumnsIndex.Brand:
DataGridViewComboBoxColumn组合框= dgvReference.Columns [(INT)ReferenceColumnsIndex.Brand]作为DataGridViewComboBoxColumn;
如果(e.ColumnIndex == comboBox.DisplayIndex)
{
IBrand品牌=新的品牌(e.FormattedValue.ToString());
如果(!brands.Contains(品牌))
{
//如果品牌不存在,将其添加到组合框$ B $的数据源b brands.Add(品牌) ;
dgvReference.NotifyCurrentCellDirty(真); //< ==此行修复该问题
}
//当设置.value的在这里的品牌,IsCurrentRowDirty()没有返回true
dgvReference.Rows [e.RowIndex] .Cells [(INT)ReferenceColumnsIndex.Brand] .value的=品牌;
}
中断;
}
}
好吧,我想通了,如何强制 IsCurrentRowDirty()
方法返回真正
。我需要添加以下代码行作为我的例子来说明:
dgvReference.NotifyCurrentCellDirty(真);
调用此对新进入的品牌将迫使该行的返回true IsCurrentRowDirty()
方法。
I have a DataGridView
with a DataGridViewComboBoxColumn
which is bound to a List<IBrand>
. In this combo box column, I allow the user to either select an existing value or type in a new one. When a user selects an existing value, IsCurrentRowDirty()
correctly returns true. When the user types in a value, the IsCurrentRowDirty()
always returns false when it obviously should return true.
I've tried calling DataGridView.CommitEdit()
in the CurrentCellDirtyStateChanged
event but that does not work
How do I get a user entered value in a DataGridViewComboBoxColumn
to set the row to dirty?
Relevant code follows.
Thanks,
Kyle
public void BindBrands()
{
DataGridViewComboBoxColumn comboBox = (DataGridViewComboBoxColumn)dgvReference.Columns[(int)ReferenceColumnsIndex.Brand];
comboBox.DisplayMember = "Name";
comboBox.ValueMember = "Self"; //"Self" returns "this"
comboBox.DataSource = brands;
}
//This enables the DataGridViewComboBoxColumn to be editable by the user
private void dgvReference_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
DataGridViewComboBoxColumn column = GetColumn<DataGridViewComboBoxColumn>(dgvReference,
(int) ReferenceColumnsIndex.Brand);
if (column != null)
{
ComboBox comboBox = e.Control as ComboBox;
if (comboBox != null)
{
comboBox.DropDownStyle = ComboBoxStyle.DropDown;
}
}
}
private void dgvReference_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
{
switch (e.ColumnIndex)
{
case (int)ReferenceColumnsIndex.Brand:
DataGridViewComboBoxColumn comboBox = dgvReference.Columns[(int)ReferenceColumnsIndex.Brand] as DataGridViewComboBoxColumn;
if (e.ColumnIndex == comboBox.DisplayIndex)
{
IBrand brand = new Brand(e.FormattedValue.ToString());
if (!brands.Contains(brand))
{
//If the brand does not exist, add it to the datasource of the combobox
brands.Add(brand);
dgvReference.NotifyCurrentCellDirty(true); //<== THIS LINE FIXES THE ISSUE
}
//When setting the .Value to brand here, IsCurrentRowDirty() does not return true
dgvReference.Rows[e.RowIndex].Cells[(int)ReferenceColumnsIndex.Brand].Value = brand;
}
break;
}
}
Okay, I figured out how to force the IsCurrentRowDirty()
method to return true
. I needed to add the following line of code as illustrated in my example:
dgvReference.NotifyCurrentCellDirty(true);
Calling this on a newly entered brand will force the row to return true for IsCurrentRowDirty()
method.
这篇关于DataGridView.IsCurrentRowDirty()不设置与编辑DataGridViewComboBoxColumn的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!