Add rows return -1 when sorted

11 posts, 0 answers
  1. Francis
    Francis avatar
    11 posts
    Member since:
    Dec 2016

    Posted 14 Dec 2018 Link to this post

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

  2. Dimitar
    Admin
    Dimitar avatar
    2909 posts

    Posted 18 Dec 2018 Link to this post

    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.
  3. Francis
    Francis avatar
    11 posts
    Member since:
    Dec 2016

    Posted 18 Dec 2018 in reply to Dimitar Link to this post

    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.
  4. Dimitar
    Admin
    Dimitar avatar
    2909 posts

    Posted 19 Dec 2018 Link to this post

    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.
  5. Francis
    Francis avatar
    11 posts
    Member since:
    Dec 2016

    Posted 19 Dec 2018 in reply to Dimitar Link to this post

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

  6. Dimitar
    Admin
    Dimitar avatar
    2909 posts

    Posted 20 Dec 2018 Link to this post

    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.
  7. Francis
    Francis avatar
    11 posts
    Member since:
    Dec 2016

    Posted 20 Dec 2018 in reply to Dimitar Link to this post

    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.
  8. Francis
    Francis avatar
    11 posts
    Member since:
    Dec 2016

    Posted 20 Dec 2018 Link to this post

    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.

  9. Francis
    Francis avatar
    11 posts
    Member since:
    Dec 2016

    Posted 20 Dec 2018 in reply to Francis Link to this post

    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.
  10. Francis
    Francis avatar
    11 posts
    Member since:
    Dec 2016

    Posted 20 Dec 2018 in reply to Francis Link to this post

    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.
  11. Dimitar
    Admin
    Dimitar avatar
    2909 posts

    Posted 21 Dec 2018 Link to this post

    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.
Back to Top