DataGridView.IsCurrentRowDirty() not set with editable DataGridViewComboBoxColumn

可紊 提交于 2020-01-04 14:31:12

问题


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

回答1:


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.



来源:https://stackoverflow.com/questions/15979905/datagridview-iscurrentrowdirty-not-set-with-editable-datagridviewcomboboxcolum

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!