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.
Regards,
Dimitar
Progress Telerik
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.
Regards,
Dimitar
Progress Telerik
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
grdData.BeginUpdate();
// remove all rows
grdData.Rows.Clear();
// add each data lines
foreach (var row in data)
{
var rowIndex = grdData.Rows.Add
(
row["CreatedBy"].ToString(),
row["IsActive"].ToString()
);
if (Convert.ToBoolean(row["IsLocked"]))
{
for (int i = 0; i < grdData.ColumnCount; i++)
{
grdData.Rows[rowIndex].Cells[i].Style.ForeColor = Color.LightGray;
}
}
}
// call end update
grdUnits.EndUpdate();
This can be implemented without using indexes. You can use the AddNew method which returns the row instance. Here is an example of this:
private
void
radButton1_Click(
object
sender, EventArgs e)
{
radGridView1.SortDescriptors.Add(
"CreatedBy"
, ListSortDirection.Descending);
radGridView1.BeginUpdate();
radGridView1.Rows.Clear();
foreach
(var row
in
source)
{
var gridRow = radGridView1.Rows.AddNew();
gridRow.Cells[
"CreatedBy"
].Value = row.CreatedBy;
gridRow.Cells[
"IsActive"
].Value = row.IsActive;
if
(row.IsLocked)
{
for
(
int
j = 0; j < radGridView1.ColumnCount; j++)
{
gridRow.Cells[j].Style.ForeColor = Color.LightGray;
}
}
}
radGridView1.EndUpdate();
}
I have attached a sample project as well.
Let me know how this works for you.
Regards,
Dimitar
Progress Telerik
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[0];
}
return selectedRow;
}
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.
Regards,
Dimitar
Progress Telerik