防止bindingsource中的重复项

时间:2015-09-14 12:18:42

标签: c#

在这段代码中,我有一个datagridview,datagridview的数据源是一个bindingsource。当我在datagridview中插入一个项目时,可以添加它2次,这样我就可以在bindingsource中获得重复的项目。

如何防止绑定源中的重复项目?“

这是我使用的代码:

    namespace WindowsFormsApplication2
    {
        public partial class Form1 : Form
        {
            class GridItem
            {
                public int Ordernummer { get; set; }
                public int Bonnummer { get; set; }
                public int Volgnummer { get; set; }
                public string Bewerking { get; set; }
            }

            List<GridItem> gridItems;
            BindingSource bs = new BindingSource();

            public Form1()
            {
                InitializeComponent();

                gridItems = new List<GridItem>{
                    new GridItem{ Ordernummer = 123456, Bonnummer = 1, Volgnummer = 40, Bewerking = "1130-Lasersnijden"},
                    new GridItem{ Ordernummer = 123678, Bonnummer = 2, Volgnummer = 30, Bewerking = "1130-Lasersnijden"}
                };

                bs.DataSource = gridItems;

                dataGridView1.RowHeadersVisible = false;
                dataGridView1.AllowUserToAddRows = false;
                dataGridView1.AllowUserToOrderColumns = false;
                dataGridView1.AllowUserToResizeRows = false;
                dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
                dataGridView1.MultiSelect = false;

                dataGridView1.AutoGenerateColumns = true;
                dataGridView1.DataSource = bs;
                dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
                dataGridView1.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells);
            }

            private void button1_Click(object sender, EventArgs e)
            {
                System.Diagnostics.Debugger.Break();
            }

            private void button2_Click(object sender, EventArgs e)
            {
//ADD ITEMS
                gridItems.Add(new GridItem{ Ordernummer = 123456, Bonnummer = 1, Volgnummer = 40, Bewerking = "1130-Lasersnijden"});
                bs.ResetBindings(false);
            }
        }
    }

注意: datagridview中的数据现在是硬编码的,在未来我将从数据库加载数据。

1 个答案:

答案 0 :(得分:1)

您可以使用 List.Contains 方法检查元素是否存在,然后再将其添加到列表中。

List.Contains metohod在内部使用List中已有的Objects的 Equals 方法,如果它们实现了IEquatable接口。

因此,您需要使您的类GridItem扩展IEquatable。

然后,覆盖方法Equals如下(在GridItem类上):

public void upload(final Context context) {
final UploadRequest request = new UploadRequest(context,
                                                "custom-upload-id",
                                                "http://www.yoursite.com/yourscript");

/*
 * parameter-name: is the name of the parameter that will contain file's data.
 * Pass "uploaded_file" if you're using the test PHP script
 *
 * custom-file-name.extension: is the file name seen by the server.
 * E.g. value of $_FILES["uploaded_file"]["name"] of the test PHP script
 */
request.addFileToUpload("/absolute/path/to/your/file",
                        "parameter-name",
                        "custom-file-name.extension",
                        "content-type"));

 try {
    //Start upload service and display the notification
    UploadService.startUpload(request);

 } catch (Exception exc) {
    //You will end up here only if you pass an incomplete UploadRequest
    Log.e("AndroidUploadService", exc.getLocalizedMessage(), exc);
 }

}

然后,在你的添加方法上:

class GridItem : IEquatable<GridItem>
    {
        public int Ordernummer { get; set; }
        public int Bonnummer { get; set; }
        public int Volgnummer { get; set; }
        public string Bewerking { get; set; }

        //This method must return true if the current GridItem and the passed as parameter are the same
        public bool Equals(GridItem gridItem)
        {
            //What does a GridItem to be the same than other?
            //If it's just Ordernummer, as easy like:
            return this.Ordernummer == gridItem.Ordernummer;
        }
    }

但是,如果您只需要使用Ordernummer属性来检查元素是否存在,您可以使用juts:

private void button2_Click(object sender, EventArgs e)
{
    //ADD ITEMS
    var gridToAdd = new GridItem{ Ordernummer = 123456, Bonnummer = 1, Volgnummer = 40, Bewerking = "1130-Lasersnijden"};
    if(!gridItems.Contains(gridToAdd){
        gridItems.Add(gridToAdd);
        bs.ResetBindings(false);
    }
}