How to avoid duplicate values in a column? is there any built functionality or else must do iteration through rows.If so in which event i can write?
karthimca07 said: How to avoid duplicate values in a column? is there any built functionality or else must do iteration through rows.If so in which event i can write?
Where exactly are you pulling a data source from? Personally, I think avoiding duplications should be taken care of at the source of the data prior to being binded to a grid. For example, if you're using data from a SQL Server table and you want to avoid duplicate values in columns, then that table should be given UNIQUE constraints for that particular column.
Depending on what kind of data source you're using there might exist a really fast optimized method for removing duplicates. Accessing rows to build some sort of HashSet/ArrayList/etc for checking in say the InitializeRow event of the UltraGrid is possible, but really inefficient.
I loaded all the details of customer in grid by assigning
grd .DataSource = dtDtl
In which i have number of columns
i'm allowing to edit email column,
so i have to validate whether any two customer has same email iD
One thing you could do is use the InitializeRow event to add each email column cell value to a HashTable, and then handle either of the events Mike suggested (BeforeCellUpdate or BeforeRowUpdate). In either of those events you can use the Contains method from your HashTable, and set e.Cancel to true if the Contains method returned true.
I would not use InitializeRow for this. InitializeRow will fire multiple times for the same row, so keeping track of your list inside that event would be pretty complicated.
If you want to build a list outside the grid and maintain, then what I would do is created your list by looping through all the rows of the grid and building the list up-front. Then you can track changes in events like BeforeRowUpdate and BeforeRowsDeleted.
That depends on when InitializeRow fires. My guess is that it will fire after the AddNew row has already been committed and so IsAddRow will be false by the time InitializeRow is called. But I could be wrong.
To me, it's clearer and more reliable to use the other events.
Mike Saltzman said: InitializeRow fires when the row is first created. So it will fire once for each root-level row when you bind the grid. But it will also fire when you change the value of any cell in any row. So this will be confusing, because you cannot just add the row to the list every time InitializeRow fires since it might already exist in the list. There's no way to determine if the event is firing because a row was added or edited or created for the first time. Also, InitializeRow will not fire at all when a row is deleted. So it's really not a good candidate for keeping a unique list of values.
InitializeRow fires when the row is first created. So it will fire once for each root-level row when you bind the grid.
But it will also fire when you change the value of any cell in any row. So this will be confusing, because you cannot just add the row to the list every time InitializeRow fires since it might already exist in the list. There's no way to determine if the event is firing because a row was added or edited or created for the first time.
Also, InitializeRow will not fire at all when a row is deleted.
So it's really not a good candidate for keeping a unique list of values.
Wouldn't it be possible to determine if the event is firing based on the eventargs passed to the event? For example:
Row Added: e.ReInitialize == false...and....e.Row.IsAddRow == true
Row Edit: e.ReInitialize == true
Row's Creation: e.ReInitialize == false
So in creating the initial HashTable would look like:
private void ultraGrid1_InitializeRow(object sender, InitializeRowEventArgs e) { if (e.ReInitialize == false && e.Row.IsAddRow == false) { //HashTable Code } }
Then use BeforeCellUpdate to handle further validation & additions, and finally use BeforeRowsDeleted to handle removing a value from the HashTable?
Mike Saltzman said: I would not use InitializeRow for this. InitializeRow will fire multiple times for the same row, so keeping track of your list inside that event would be pretty complicated. If you want to build a list outside the grid and maintain, then what I would do is created your list by looping through all the rows of the grid and building the list up-front. Then you can track changes in events like BeforeRowUpdate and BeforeRowsDeleted.
What causes the refiring of InitializeRow for the current row if you're retrieving a cell value to hash out to a HashTable? Or did I just answer my own question; the retrieving of the cell value causes this? This will be useful for my own projects and even answering future questions.