Just noticed that when the grid as a sorting applied the following line ALWAYS return -1
var rowIndex = grdData.Rows.Add("whatever");
Without sorting it return the actual row. What i actually do is :
1 - Call the Grid.BeginUpdate();
2 - Clear the rows
3 - Loop in my collection and add 1 row per object
4 - Call the Grid.EndUpdate()
10 Answers, 1 is accepted
This is expected because the sorting is not performed until the EndUpdate method is reached. It is faster to sort the rows at once than sorting after each row is added. In this case, you can iterate the rows after the grid is populated and get their indexes.
I hope this will be useful. Let me know if you have additional questions.
Should this be done while adding the rows? Can you give your entire code for adding the rows so I can get a better understanding of your exact case?
If this should be done when the rows are added you can either remove the sorting or the Begin/End update block.
I am looking forward to your reply.
The begin and end update cannot be removed. It has been given by one of you colleague to bypass an exception that was throwing sometime. Sorting cannot be removed. I has to stay.
// get data
var data = GetData(selectedProject);
// prepare for update
// remove all rows
// add each data lines
foreach (var row in data)
var rowIndex = grdData.Rows.Add
for (int i = 0; i < grdData.ColumnCount; i++)
grdData.Rows[rowIndex].Cells[i].Style.ForeColor = Color.LightGray;
// call end update
This can be implemented without using indexes. You can use the AddNew method which returns the row instance. Here is an example of this:
I have attached a sample project as well.
Let me know how this works for you.
Actually lots of other code started to stop working by changing this.
When i click on a row i have an event that i get the selected row. I made a generic method (below) that always return the first row.
private GridViewRowInfo GetSelectedRow(RadGridView grid)
GridViewRowInfo selectedRow = null;
if (grid.SelectedRows != null && grid.SelectedRows.Count == 1)
selectedRow = grid.SelectedRows;
When i pass my grid it return me 1 row but all the cells.values are null. If i revert back to using grid.Rows.Add() instead of AddNew() the cells.Value are NOT empty anymore.
I am glad that this works now. I wanted to note that the IsLocked field can be added to the grid as well. This will allow you to use the other styling mechanisms like Conditional Formatting and CellFormatting.
Do not hesitate to contact us if you have other questions.