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

Set CurrentRow failing Q3 2007

9 Answers 136 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Justin
Top achievements
Rank 1
Justin asked on 12 Feb 2008, 07:41 PM
I'm using the following code to set the current row in the RadGridView. When I set the CurrentRow property, then get it directly after, I'm finding that the CurrentRow is never the same as the row that I set. The code is always returning true though, so the set is happening. I've tried this after the DataBound event happens and still it doesn't work. What am I doing wrong?

public bool SetCurrentRow(DataRow r, string fieldname, object val)
        {
            if ((this.Columns.FindByDataField(fieldname) == null
                 || r[fieldname] == null))
            {
                string m = string.Format("{0} failed. Grid columns or row columns did not contain '" + fieldname + "'."
                  , "SetCurrentGridRow");
                throw new ApplicationException(m);
            }

            System.Diagnostics.Debug.WriteLine("");

            System.Diagnostics.Debug.WriteLine("Looking for: "+ r[fieldname].ToString());

            foreach (GridViewRowInfo v in this.Rows)
            {
                if (v.DataBoundItem is DataRowView)
                {
                    if ((v.DataBoundItem as DataRowView).Row != null)
                    {

                        DataRow row = (v.DataBoundItem as DataRowView).Row;
                        System.Diagnostics.Debug.WriteLine(row[fieldname]);
                        if (row[fieldname].Equals(val))
                        {
                            this.CurrentRow = v;
                            this.GridElement.Update(false);
                            return true;
                        }                      
                    }
                }
            }

            return false;
        }

I've even tried this.CurrentRow = this.Rows[2] and it's not working.

9 Answers, 1 is accepted

Sort by
0
Jack
Telerik team
answered on 13 Feb 2008, 12:18 PM
Hi Justin,

Thank you for writing us.

We addressed this issue in our latest version Q3 2007 SP1. You can download it from your client account.

Do not hesitate to contact us if you have other questions.

Sincerely yours,
Jack
the Telerik team

Instantly find answers to your questions at the new Telerik Support Center
0
Justin
Top achievements
Rank 1
answered on 15 Feb 2008, 01:39 AM

I don't think this is fixed. Directly after the this.GridElement.Update(false) in the above code, I had the following values:

this.Rows.IndexOf(v)  
272  
this.Rows.IndexOf(this.CurrentRow)  
0  
 

Welp, there you are. Even setting directly (this.CurrentRow = this.Rows[272]) didn't work, I got a IndexOf = 0. I'm using version 5.0.3.0 which I am pretty sure is Q3 07 SP1.
0
Jack
Telerik team
answered on 15 Feb 2008, 10:40 AM
Hello Justin,

Unfortunately, we could not reproduce this issue with the Q3 2007 SP1 release.

Please consider that when grouping is applied, the Rows collection of the RadGridView contains only the group rows at the first level. You should iterate through the Rows collection in each group in order to find the correct row.

The SetCurrentRow function below demonstrates this behavior:

 
SetCurrentRow(this.radGridView1.Rows, "Name""Row 50"); 
public bool SetCurrentRow(GridViewRowCollection rows, string fieldName, object value) 
    if (value != null
    { 
        foreach (GridViewRowInfo rowInfo in rows) 
        { 
            if (value.Equals(rowInfo.Cells[fieldName].Value)) 
            { 
                rowInfo.IsCurrent = true
                return true
            } 
            if (SetCurrentRow(rowInfo.Rows, fieldName, value)) 
            { 
                break
            } 
        } 
    } 
    return false

Please tell us if this solves your issue. We will be glad to help you further.

Kind regards,
Jack
the Telerik team

Instantly find answers to your questions at the new Telerik Support Center
0
Justin
Top achievements
Rank 1
answered on 15 Feb 2008, 07:27 PM

I've tried putting this code in a variety of places. The most obvious one is the DataBindingComplete event. I don't need the recursion since I'm not using a tiered grid. The code does find the newly added row, but setting the IsCurrent prop and the CurrentRow prop never actually work. I've even tried putting that code in the _Paint event of the grid, thinking that all the rows would certainly have to be there at that time.

if (newRowValue != null)  
            {  
                string fieldName = "ID";  
                GridViewRowCollection rows = products.Rows;  
                if (rows.Count == 0) return;                  
                    System.Diagnostics.Debug.WriteLine("Last row id: "   
                        + rows[rows.Count - 1].Cells[fieldName].Value.ToString());  
                    foreach (GridViewRowInfo rowInfo in rows)  
                    {  
                        if (rowInfo.Cells[fieldName].Value.Equals(newRowValue))  
                        {  
                            rowInfo.IsCurrent = true;  
                            products.CurrentRow = rowInfo;  
 
                            System.Diagnostics.Debug.WriteLine("Current row value: " +                
                                rowInfo.Cells[fieldName].Value.ToString());  
                            System.Diagnostics.Debug.WriteLine("Is current: " + rowInfo.IsCurrent.ToString());  
 
                            newRowValue = null;                               
                            return;  
                        }  
                    }                
            }  
 
And my output:   

Last row id: 567 
Current row value: 567 
Is current: False

So this is broken. Maybe you can send me a code sample wherein this is actually working? My grid has about 200 rows in it.
0
Jack
Telerik team
answered on 18 Feb 2008, 01:18 PM
Hello Justin,

I am regret to hear that we could not help solving this issue.

The DataBindingComplete event is not a good place for setting the CurrentRow, because there may be fired other ListChanged events after processing the DataBindingEvent. These will reset your changes. Could you please send us your application, we will try to find the best place for setting the CurrentRow.

Do not hesitate to write us if you have other questions.

Kind regards,
Jack
the Telerik team

Instantly find answers to your questions at the new Telerik Support Center
0
Justin
Top achievements
Rank 1
answered on 20 Feb 2008, 01:30 AM
OK, I've managed to put together a simple app that shows this issue. I can't send you the app that I'm working with but this one shows the issue. 

http://www.content-type.com/1850164681/TelerikGridTest.zip.htm

To recreate:
- load app
- sort by ID desc
- scroll down to about ID 40
- select a row
- hit add new row 
    - new row is added but not selected
- hit add new row again
   - new row is added and selected, but still not in view
0
Accepted
Jack
Telerik team
answered on 20 Feb 2008, 06:12 PM
Hello Justin,

Thank you for sending me your project.

I managed to reproduce the issue and found a solution for you. Check the source files attached here.
I will briefly describe the reasons for this issue:

You are using the following line of code to generate a new row:

DataRow r = GenerateNewRow(this.currentRowID++); 

This is alright, but after this line the currentRowID is updated and contains a new value. This value is valid only for the next row that will be added to the table. Because of this, the SetCurrentRow method is never successful.

In your sample, after generating a new row, the DataSource is set. This last step is not necessary, because the RadGridView will update its content automatically.

I also found an issue inside the EnsureVisible method of the GridViewRowInfo. In your case it fails to scroll to the newly added row. We will address this issue in our upcoming release Q1 2008. I added the following code inside the SetCurrentRow method to workaround with this issue for the time being:

if (rowInfo.VisualElement == null
     this.radGridView1.GridElement.ScrollToRow(rowInfo); 
 

I hope this helps. Please let us know if you need further assistance.

Regards,
Jack
the Telerik team

Instantly find answers to your questions at the new Telerik Support Center
0
Justin
Top achievements
Rank 1
answered on 20 Feb 2008, 11:05 PM
You may have been right about that postfix, I changed that line, but must have been after I sent it to you. The code was definitely finding the row, it looks like the ScrollToRow was what I was needing. Sounds like you need a "SetCurrentRow" with an overload for scrolling, instead of allowing the CurrentRow to be set.
0
Jack
Telerik team
answered on 21 Feb 2008, 11:18 AM
Hi Justin,

Thank you for this suggestion.

We will consider adding such functionality in RadGridView in a future release of RadControls. Actually when the CurrentRow is set, the grid must be scrolled to that row. We will address this issue in our upcoming release Q1 2008.

Do not hesitate to write me, if you have other questions.

Greetings,
Jack
the Telerik team

Instantly find answers to your questions at the new Telerik Support Center
Tags
GridView
Asked by
Justin
Top achievements
Rank 1
Answers by
Jack
Telerik team
Justin
Top achievements
Rank 1
Share this question
or