Prevent Duplicate Values

4 posts, 0 answers
  1. ShareDocs
    ShareDocs avatar
    91 posts
    Member since:
    Oct 2012

    Posted 29 Jan 2013 Link to this post

    Hi,
    i have a grid with a GridViewDateTimeColumn. I want to prevent the user from inserting a date which is already exists so i don't have duplicates on the gridview (or the DB). how can i achieve that?

    BTW:
    Can you direct me to a good example of the GridViewDateTimeColumn.DistinctValues property? it seems like i can use it but i don't know how.

    Thanks
  2. Julian Benkov
    Admin
    Julian Benkov avatar
    1135 posts

    Posted 31 Jan 2013 Link to this post

    Hi Lior,

    Thank you for writing.

    You can use the SortedSet collection and CellValidating event to implement this functionality. Here is a example:
    using System;
    using System.Collections.Generic;
    using System.Windows.Forms;
    using Telerik.WinControls.UI;
     
    namespace Lab.Grid
    {
        public partial class GridUniqueValuesForm : Form
        {
            private RadGridView gridView = new RadGridView();
            private SortedSet<DateTime> values = new SortedSet<DateTime>();
     
            public GridUniqueValuesForm()
            {
                InitializeComponent();
     
                gridView.Dock = DockStyle.Fill;
                gridView.Parent = this;
     
                gridView.ColumnCount = 3;
                gridView.RowCount = 10;
                GridViewDateTimeColumn dateColumn = new GridViewDateTimeColumn("MyColumn, MyColumn");
                 
                gridView.Columns.Add(dateColumn);
                gridView.ShowHeaderCellButtons = true;
                gridView.EnableFiltering = true;
                gridView.CellValidating += gridView_CellValidating;
            }
     
            void gridView_CellValidating(object sender, CellValidatingEventArgs e)
            {
                if(e.ColumnIndex == 3 && e.Value is DateTime)
                {
                    DateTime date = DateTime.Parse(e.Value.ToString());
     
                    if (values.Contains(date))
                    {
                        gridView.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = null;
                        MessageBox.Show("My DateTime column contains this date.");
                    }
                    else
                    {
                        values.Add(date);
                    }
                }
     
                if (e.ColumnIndex == 3 && e.OldValue is DateTime)
                {
                    DateTime date = DateTime.Parse(e.OldValue.ToString());
                    values.Remove(date);
                }
            }
        }
    }

    I hope this helps. Do not hesitate to contact us if you have further questions or issues.

    Greetings,
    Julian Benkov
    the Telerik team
    Q3'12 SP1 of RadControls for WinForms is out now. See what's new.
  3. ShareDocs
    ShareDocs avatar
    91 posts
    Member since:
    Oct 2012

    Posted 03 Feb 2013 Link to this post

    Hi,
    This solution isn't good for me since it assumes a blank grid and i use insert a DataTable to fill the grid before i can edit/insert the grid.

    I thought there is a better way to find value inside the grid's collection (rather then generating an outside list).
    anyway, this is the solution i implemented eventually:
    void grid_CellValidating(object sender, CellValidatingEventArgs e)
     {
     if (e.Value != null && e.Value != System.DBNull.Value && this.grdHafazot.IsInEditMode)
     {
     if (e.Column.Name == "ColumnName")
     string filterExp = e.OldValue != null ? "ColumnName = " + e.Value.ToString() + "and ColumnName <> " : "ColumnName = " e.OldValue.ToString()
     if ((grid.DataSource as DataTable).Select(filterExp).Length > 0)
     {
     e.Cancel = true;
     this.grid.ActiveEditor.Value = e.OldValue;
     }
     }
     }

     

     

     

     

  4. Julian Benkov
    Admin
    Julian Benkov avatar
    1135 posts

    Posted 07 Feb 2013 Link to this post

    Hi Lior,

    Thank you for writing me back. I am glad to hear that you have found a solution that fits in your scenario. We still think that the best option in this scenario is to use an external collection. This way you will not observe performance issues when binding to a large DataTable.

    Do not hesitate to contact us if you have further questions or issues.

    Kind regards,
    Julian Benkov
    the Telerik team
    Q3'12 SP1 of RadControls for WinForms is out now. See what's new.
Back to Top