我很难解决这个问题。我有一个名为Section的对象,我将此Section对象添加到列表中,然后该列表用作数据网格视图数据源。当我将对象添加到列表中时,一切都很好,当我设置DataGridView数据源并将其显示在网格上时,一切正常。当我添加其他类型为Section的Object时,将List设置为网格数据源后,网格将显示2个对象,但是这两个对象与添加的第一个对象相同。添加第三个对象的行为相同,依此类推。是什么原因造成的?绑定到datagridview时更改数据源列表?
这里是设置对象的部分
private void AddSectionButtonClicked(object sender, EventArgs e) {
try {
var documentType = MopDocument.DocumentType;
var sections = FillAssemblyInformation(documentType.DllPath);
if (sections == null || sections.Count <= 0)
return;
var addSectionForm = new MopSectionAddEditForm(sections);
var dialog = addSectionForm.ShowDialog();
if (dialog == DialogResult.Cancel)
return;
var addedSection = addSectionForm.AddedEditedMopSection;
var sectionsList = dgvSelectedSections.DataSource as BindingList<MopSection>;
if (sectionsList == null || sectionsList.Count == 0) {
sectionsList = new BindingList<MopSection>();
addedSection.SequenceNumber = 1;
} else {
var lastLevel = sectionsList[sectionsList.Count - 1].SequenceNumber;
addedSection.SequenceNumber = lastLevel + 1;
}
sectionsList.Add(addedSection);
// I looped this list to check what objects are being added, and it is correct.
dgvSelectedSections.DataSource = null;
dgvSelectedSections.DataSource = sectionsList;
// I looped again on the list and now the objects are duplicated to the first element that was added to the list
dgvSelectedSections.Rows[dgvSelectedSections.Rows.Count - 1].Selected = true;
} catch (Exception ex) {
Logger.ShowErrorDialog("EXception Occured", ex);
}
}
请告知您为什么会发生这种情况
答案 0 :(得分:0)
发现解决方案,这是由于在Section Objects中重写的.Equals方法中的错误检查导致的。