首页 > 解决方案 > C# 获取已删除的项目和更改的项目以列出

问题描述

我有一个问题要将所有已删除的项目以及已更改的项目添加到新列表中。我正在使用带有可排序绑定源的数据网格视图。对于所有新项目,它已经在工作(代码的最后一部分)

谢谢!

namespace Levelapp
{
    public partial class LevelView : Form
    {
        FilterLevel m_filterLevel;
        int m_filterLevelTotal;

        public LevelView()
        {
            InitializeComponent();
        }

        public LevelView(FilterLevel opt)
        {
            InitializeComponent();

            m_filterLevel = opt;

            bindingSource1.DataSource = typeof(LevelResource);
            dataGridView1.DataSource = bindingSource1;            
            bindingSource1.DataSource = m_filterLevel.FoundLevels;

            m_filterLevelTotal = bindingSource1.Count;

        }

        private void newSheet_Click(object sender, EventArgs e)
        {
            string newItemName = "Sheet" + " " + "1";
            string newItemNumber = "A-00";

            LevelResource newItem = new LevelResource();
            newItem.Name = newItemName;
            newItem.Number = newItemNumber;

            bindingSource1.Add(newItem);
        }

        private void deleteSheet_Click(object sender, EventArgs e)
        {
            bindingSource1.RemoveCurrent();
        }

        private void ok_Click(object sender, EventArgs e)
        {
            for (int i = m_filterLevelTotal; i < bindingSource1.Count; i++)
            {
                bindingSource1.Position = i;
                LevelResource newSheet = bindingSource1.Current as LevelResource;             
            }
        }
    }
}

感谢您及时的回复。但我在 bool 结果行出现错误。此代码将在 Revit 中使用。在我如何将您的代码放在 deleteSheet 按钮下的部分下方

private void deleteSheet_Click(object sender, EventArgs e)
{

    for (int i = 0; i < bindingSource1.Count; i++)
    {
        bindingSource1.Position = i;
        var view = bindingSource1.Current as DataRowView;
        bool result = view.Row.RowState == DataRowState.Added || view.Row.RowState == DataRowState.Unchanged;
        if (result)
        {
            // new or didn't modified, work as normal

        }
        else
        {
            // add to another list
        }
    }            
}

Revit 错误

标签: c#datagridview

解决方案


推荐阅读