DataGridViewComboBoxColumn value change not changing RowState to modified

ぐ巨炮叔叔 提交于 2020-01-16 16:27:59

问题


I have added a lookup combobox to my datagridview. Any changed to an existing row or adding a new row changed the value in RowState on save to Modified or Added. except changing a value in the combobox. On save, the RowState remains as unmodified.

the code i use to add the combobox is.

 DataGridViewComboBoxColumn cbQualification = new DataGridViewComboBoxColumn();
            cbQualification.HeaderText = "Course Code";
            DataSet  myDataSet = GetData.GetCoursesData();
            cbQualification.DataSource = myDataSet.Tables[0];
            cbQualification.DisplayMember = "Code";
            cbQualification.ValueMember = "ID";
            cbQualification.DataPropertyName = "QualID";
            grdPersonQuals.Columns.Insert (1,cbQualification);

the save event uses the code.

 grdPersonQuals.BindingContext[grdPersonQuals.DataSource, grdPersonQuals.DataMember].EndCurrentEdit();
            foreach (DataRow row in dsPersonQuals.Tables[0].Rows)
            {
                object x = row.RowState;
            }

回答1:


I'm guessing the focus is still in your combobox column when hitting your save button? I've always called the DataGridView's EndEdit method to trigger updating the datasource.

So in your save button event

grdPersonQuals.EndEdit();

You are calling it on the binding context, but I believe you need to call it on the grid itself so it pushes the changes in the grid down to it's datasource.




回答2:


You may set rowstate if it is unmodified

    foreach (DataRow row in dsPersonQuals.Tables[0].Rows)
        {
            row.SetAdded(); // or row.SetModified();
            object x = row.RowState;
        }


来源:https://stackoverflow.com/questions/13119362/datagridviewcomboboxcolumn-value-change-not-changing-rowstate-to-modified

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