2

我有一个DataGridView与 aDataGridViewComboBoxColumn绑定的 a List<IBrand>。在此组合框列中,我允许用户选择现有值或键入新值。当用户选择现有值时,IsCurrentRowDirty()正确返回 true。当用户输入一个值时,IsCurrentRowDirty()当它显然应该返回 true 时,总是返回 false。

我已经尝试DataGridView.CommitEdit()CurrentCellDirtyStateChanged事件中调用,但这不起作用

如何让用户在 a 中输入值DataGridViewComboBoxColumn以将行设置为脏?

相关代码如下。

谢谢,

凯尔

        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;
    }
}
4

1 回答 1

2

好的,我想出了如何强制IsCurrentRowDirty()方法返回true。我需要添加以下代码行,如我的示例所示:

dgvReference.NotifyCurrentCellDirty(true);

对新输入的品牌调用此方法将强制该行为方法返回 true IsCurrentRowDirty()

于 2013-04-16T18:09:19.610 回答