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

Add rows return -1 when sorted

10 Answers 40 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Francis
Top achievements
Rank 1
Francis asked on 14 Dec 2018, 05:11 PM

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

Sort by
0
Dimitar
Telerik team
answered on 18 Dec 2018, 11:25 AM
Hi Francis,

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
Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
0
Francis
Top achievements
Rank 1
answered on 18 Dec 2018, 12:28 PM
How am i supposed to figure out the index. Some rows are duplicates but their orders were important. I need to know that index 2 and 3 that were duplicate refers to row 5 and 6 and need to know if 5 is 2 or if it's 3.
0
Dimitar
Telerik team
answered on 19 Dec 2018, 08:51 AM
Hello Francis,

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
Get quickly onboard and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
0
Francis
Top achievements
Rank 1
answered on 19 Dec 2018, 02:49 PM

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();

0
Dimitar
Telerik team
answered on 20 Dec 2018, 09:04 AM
Hello Francis,

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
Get quickly onboard and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
0
Francis
Top achievements
Rank 1
answered on 20 Dec 2018, 01:21 PM
It works, But i had to change the way i created the columns. I was creating the columns by setting the fieldname AND the uniquename. This method doesn't work when both are setted. You must only set the fieldname and not the uniquename.
0
Francis
Top achievements
Rank 1
answered on 20 Dec 2018, 01:29 PM

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.

0
Francis
Top achievements
Rank 1
answered on 20 Dec 2018, 01:35 PM
Actually it has nothing to do with the SelectedRow. Right after adding data to the grid if i go in the Grid.Rows and all cells no matter which one i look at they all have Null in Value but strangely visually i do see all the sample text string i putted there. If i use Rows.Add() instead of Rows.AddNew() the Value contain the string i putted.
0
Francis
Top achievements
Rank 1
answered on 20 Dec 2018, 03:26 PM
We just got our work around fix completed. I replaced the control and it works now and it's much more intuitive. Sorry for wasting your time.
0
Dimitar
Telerik team
answered on 21 Dec 2018, 12:23 PM
Hi Francis,

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
Get quickly onboard and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
Tags
GridView
Asked by
Francis
Top achievements
Rank 1
Answers by
Dimitar
Telerik team
Francis
Top achievements
Rank 1
Share this question
or