This is a migrated thread and some comments may be shown as answers.

Prevent Duplicate Values

3 Answers 482 Views
GridView
This is a migrated thread and some comments may be shown as answers.
ShareDocs
Top achievements
Rank 1
ShareDocs asked on 29 Jan 2013, 10:41 AM
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

3 Answers, 1 is accepted

Sort by
0
Julian Benkov
Telerik team
answered on 31 Jan 2013, 02:33 PM
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.
0
ShareDocs
Top achievements
Rank 1
answered on 03 Feb 2013, 06:37 AM
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;
 }
 }
 }

 

 

 

 

0
Julian Benkov
Telerik team
answered on 07 Feb 2013, 09:37 AM
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.
Tags
GridView
Asked by
ShareDocs
Top achievements
Rank 1
Answers by
Julian Benkov
Telerik team
ShareDocs
Top achievements
Rank 1
Share this question
or