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?
If I have understood well your question, I found a very simple validation with the following code in the BeforeRowUpdate event:
For i As Int32 = 1 To ugDef1.Rows.Count - 1 If e.Row.Cells(0).Value = ugDef1.Rows(i).Cells(0).Value Then e.Cancel = True : Exit Sub Next
I hope this will help you
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.
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.