设置DGV数据源[C#]后复制行(对象)
我很难解决这个问题。我有一个名为Section的对象,我正在将这个Section对象添加到一个列表中,然后该列表被用作数据网格视图数据源。当我将对象添加到列表中时,一切正常;当我设置DataGridView数据源并在网格上显示它时,一切正常。当我添加另一个Section类型的对象时,只要将列表设置为网格数据源,网格将显示2个对象,但这两个对象与添加的第一个对象相同。添加第三个对象会执行相同的行为,依此类推。这可能是什么原因造成的?在数据源列表绑定到datagridview时更改它 下面是设置对象的部分设置DGV数据源[C#]后复制行(对象),c#,list,datagridview,binding,datasource,C#,List,Datagridview,Binding,Datasource,我很难解决这个问题。我有一个名为Section的对象,我正在将这个Section对象添加到一个列表中,然后该列表被用作数据网格视图数据源。当我将对象添加到列表中时,一切正常;当我设置DataGridView数据源并在网格上显示它时,一切正常。当我添加另一个Section类型的对象时,只要将列表设置为网格数据源,网格将显示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);
}
}
private void AddSectionButtonClicked(对象发送方,事件参数e){
试一试{
var documentType=MopDocument.documentType;
var sections=FillAssemblyInformation(documentType.DllPath);
如果找到(sections==null | | sections.Count解决方案,则发生这种情况的原因是在Section对象中重写的.Equals方法中检查错误