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

radgridview.Rows.Add does not return the row number

9 Answers 119 Views
GridView
This is a migrated thread and some comments may be shown as answers.
KARTHIK REDDY KAMIDI
Top achievements
Rank 1
KARTHIK REDDY KAMIDI asked on 08 Feb 2011, 07:26 PM
Hello,

I have some code that worked up until Q2 2010 SP1, which no longer works in Q3 2010. RadGridView.Rows.Add(...) used to return the row index of the added row, but in Q3 2010 it returns -1.

I have a hierarchical GridView and the following code:

private void SetupRadGridviewRelation()
{
    GridViewRelation selfRelation = new GridViewRelation(this.radGridView1.MasterTemplate, this.radGridView1.MasterTemplate);
    selfRelation.ParentColumnNames.Add("IdCol");
    selfRelation.ChildColumnNames.Add("ParentIdCol");
    this.radGridView1.Relations.Add(selfRelation);
}
 public void LoadVariables
 {  
    foreach (var varModel in list)
    {
        var rowNum = radGridView1.Rows.Add(varModel.Id, varModel.ParentId, varModel.VarName, varModel.VarValue, varModel.VarUnits);
        radGridView1.Rows[rowNum].Tag = varModel;
    }
 }


Does anybody know of a solution. Is this a bug in Q3 2010?

Thanks.

9 Answers, 1 is accepted

Sort by
0
Accepted
Richard Slade
Top achievements
Rank 2
answered on 09 Feb 2011, 12:30 AM
Hello,

You need to use the following code (DeferRefresh) in order for this to work correctly

using (this.radGridView1.DeferRefresh())
     foreach (var varModel in list)
    {
        var rowNum = radGridView1.Rows.Add(varModel.Id, varModel.ParentId, varModel.Name, varModel.Value, varModel.Units);
        radGridView1.Rows[rowNum].Tag = varModel;
        if (rowNum < 0) { MessageBox.Show(rowNum.ToString()); }
    }               
}

Here is a full exmaple you can paste into a new project

Designer File
namespace RadGridView_Hierarchy_CS
{
    partial class Form1
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components;
  
        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }
  
        #region Windows Form Designer generated code
  
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.radGridView1 = new Telerik.WinControls.UI.RadGridView();
            ((System.ComponentModel.ISupportInitialize)(this.radGridView1)).BeginInit();
            this.SuspendLayout();
            // 
            // radGridView1
            // 
            this.radGridView1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.radGridView1.Location = new System.Drawing.Point(0, 0);
            this.radGridView1.Name = "radGridView1";
            this.radGridView1.Size = new System.Drawing.Size(323, 320);
            this.radGridView1.TabIndex = 0;
            this.radGridView1.Text = "radGridView1";
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(323, 320);
            this.Controls.Add(this.radGridView1);
            this.Name = "Form1";
            this.Text = "Form1";
            this.Load += new System.EventHandler(this.Form1_Load);
            ((System.ComponentModel.ISupportInitialize)(this.radGridView1)).EndInit();
            this.ResumeLayout(false);
  
        }
  
        #endregion
  
        private Telerik.WinControls.UI.RadGridView radGridView1;
    }
}

Form1.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Telerik.WinControls.UI;
  
  
namespace RadGridView_Hierarchy_CS
{
    public partial class Form1 : Form
    {
  
        private BindingList<Model> list = new BindingList<Model>();
  
        public Form1()
        {
            InitializeComponent();
  
            this.radGridView1.AutoGenerateColumns = false;
            this.radGridView1.AutoGenerateHierarchy = false;
  
            this.radGridView1.Columns.Add(new GridViewDecimalColumn("Id"));
            this.radGridView1.Columns.Add(new GridViewDecimalColumn("ParentId"));
            this.radGridView1.Columns.Add(new GridViewTextBoxColumn("Name"));
            this.radGridView1.Columns.Add(new GridViewTextBoxColumn("Value"));
            this.radGridView1.Columns.Add(new GridViewDecimalColumn("Units"));
  
  
            list.Add(new Model(1, 0, "Parent 1", "Value 1", 1));
            list.Add(new Model(2, 0, "Parent 2", "Value 2", 1));
            list.Add(new Model(3, 0, "Parent 3", "Value 3", 1));
  
            list.Add(new Model(4, 1, "Child 1", "Value 1", 1));
            list.Add(new Model(5, 2, "Child 2", "Value 2", 1));
            list.Add(new Model(6, 3, "Child 3", "Value 3", 1));
        }
  
         
        private void Form1_Load(object sender, EventArgs e)
        {
            SetupRadGridviewRelation();
            LoadVariables();
        }   
  
        private void SetupRadGridviewRelation()
        {
            this.radGridView1.Relations.AddSelfReference(this.radGridView1.MasterTemplate, "Id", "ParentId");
        }
  
        public void LoadVariables()
        {
            using (this.radGridView1.DeferRefresh())
            
                 foreach (var varModel in list)
                {
                    var rowNum = radGridView1.Rows.Add(varModel.Id, varModel.ParentId, varModel.Name, varModel.Value, varModel.Units);
                    radGridView1.Rows[rowNum].Tag = varModel;
                    if (rowNum < 0) { MessageBox.Show(rowNum.ToString()); }
                }               
            }
  
        }
    }
  
    public class Model
    {
        public Model() { }
  
        public Model(int id, int parentId, string name, string value, int units)
        { this.Id = id; this.ParentId = parentId; this.Name = name; this.Value = value; this.Units = units; }
  
        public int Id
        { get; set; }
  
        public int ParentId
        { get; set; }
  
        public string Name
        { get; set; }
  
        public string Value
        { get; set; }
  
        public int Units
        { get; set; }
    }
}

Hope that helps
Richard
0
KARTHIK REDDY KAMIDI
Top achievements
Rank 1
answered on 16 Feb 2011, 05:58 PM
Richard,

Thank you very much for your reply and the complete source code. The solution that you provided works in the sample project and I was able to see it working in a test project that I had created based on your solution. However, adding the changes to my main project still had issues. I have to see why it is working in the sample projects and not in the main project.

Thanks.
0
Richard Slade
Top achievements
Rank 2
answered on 16 Feb 2011, 06:01 PM
Hi,

Im glad you found that useful and you see it working in the sample. If you have difficultes with your own one though, then let me know and post a sample that replicates it and I'll do my best to help
Regards,
Richard
0
KARTHIK REDDY KAMIDI
Top achievements
Rank 1
answered on 22 Feb 2011, 11:02 PM
Richard,

I now can replicate the issue in your sample project also. Could you try adding            
this.radGridView1.Rows.Clear();
to clear the existing rows before adding in the rows in the LoadVariables() method?

After this call, it consistently returns -1 in radGridView1.Rows.Add(...); I need to know which row was added so that I can set the tag on the row to refer to my Model object.

Thanks.
0
Richard Slade
Top achievements
Rank 2
answered on 22 Feb 2011, 11:15 PM
Hello,

I've added this into the place that you mentioned and the message box still does not appear to say that the row index is -1. Please could you alter the sample that I posted above from before to replicate the issue and then re-post using the format code block again here?
Thanks
Richard
0
KARTHIK REDDY KAMIDI
Top achievements
Rank 1
answered on 23 Feb 2011, 12:08 AM
Hello,

Thank you for looking into this issue. The code is as follows:

namespace RadGridView_Hierarchy_CS
{
    partial class Form1
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components;
 
        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }
 
        #region Windows Form Designer generated code
 
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.radGridView1 = new Telerik.WinControls.UI.RadGridView();
            ((System.ComponentModel.ISupportInitialize)(this.radGridView1)).BeginInit();
            this.SuspendLayout();
            //
            // radGridView1
            //
            this.radGridView1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.radGridView1.Location = new System.Drawing.Point(0, 0);
            this.radGridView1.Name = "radGridView1";
            this.radGridView1.Size = new System.Drawing.Size(323, 320);
            this.radGridView1.TabIndex = 0;
            this.radGridView1.Text = "radGridView1";
            //
            // Form1
            //
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(323, 320);
            this.Controls.Add(this.radGridView1);
            this.Name = "Form1";
            this.Text = "Form1";
            this.Load += new System.EventHandler(this.Form1_Load);
            ((System.ComponentModel.ISupportInitialize)(this.radGridView1)).EndInit();
            this.ResumeLayout(false);
 
        }
 
        #endregion
 
        private Telerik.WinControls.UI.RadGridView radGridView1;
    }
}
 
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Telerik.WinControls.UI;
 
 
namespace RadGridView_Hierarchy_CS
{
    public partial class Form1 : Form
    {
 
        private BindingList<Model> list = new BindingList<Model>();
 
        public Form1()
        {
            InitializeComponent();
 
            this.radGridView1.AutoGenerateColumns = false;
            this.radGridView1.AutoGenerateHierarchy = false;
 
            this.radGridView1.Columns.Add(new GridViewDecimalColumn("Id"));
            this.radGridView1.Columns.Add(new GridViewDecimalColumn("ParentId"));
            this.radGridView1.Columns.Add(new GridViewTextBoxColumn("Name"));
            this.radGridView1.Columns.Add(new GridViewTextBoxColumn("Value"));
            this.radGridView1.Columns.Add(new GridViewDecimalColumn("Units"));
 
            list.Add(new Model(1, 0, "Parent 1", "Value 1", 1));
            list.Add(new Model(2, 0, "Parent 2", "Value 2", 1));
            list.Add(new Model(3, 0, "Parent 3", "Value 3", 1));
 
            list.Add(new Model(4, 1, "Child 1", "Value 1", 1));
            list.Add(new Model(5, 2, "Child 2", "Value 2", 1));
            list.Add(new Model(6, 3, "Child 3", "Value 3", 1));
        }
 
 
        private void Form1_Load(object sender, EventArgs e)
        {
            SetupRadGridviewRelation();
            LoadVariables();
        }
 
        private void SetupRadGridviewRelation()
        {
            this.radGridView1.Relations.AddSelfReference(this.radGridView1.MasterTemplate, "Id", "ParentId");
        }
 
        public void LoadVariables()
        {
            this.radGridView1.Rows.Clear();
 
            using (this.radGridView1.DeferRefresh())
            {
                foreach (var varModel in list)
                {
                    var rowNum = radGridView1.Rows.Add(varModel.Id, varModel.ParentId, varModel.Name, varModel.Value, varModel.Units);
                    if (rowNum < 0)
                        MessageBox.Show(rowNum.ToString());
                    else
                        radGridView1.Rows[rowNum].Tag = varModel;
 
                }
            }
 
        }
    }
 
    public class Model
    {
        public Model() { }
 
        public Model(int id, int parentId, string name, string value, int units)
        { this.Id = id; this.ParentId = parentId; this.Name = name; this.Value = value; this.Units = units; }
 
        public int Id
        { get; set; }
 
        public int ParentId
        { get; set; }
 
        public string Name
        { get; set; }
 
        public string Value
        { get; set; }
 
        public int Units
        { get; set; }
    }
}


Thanks
0
Accepted
Richard Slade
Top achievements
Rank 2
answered on 23 Feb 2011, 01:17 AM
Hello,

In order to clear the rows first you need to put the Rows.Clear inside the DeferRefresh()
All update operations that affect the rows should be done inside this. Internally it suspends notifications whilst the Begin/End update is taking place which will break if you add the Rows.Clear outside of this.

Hope this helps
Richard

Edit// Adding code sample
public void LoadVariables()
{
    
    using (this.radGridView1.DeferRefresh())
    {
        this.radGridView1.Rows.Clear();  
        foreach (var varModel in list)
        {
            var rowNum = radGridView1.Rows.Add(varModel.Id, varModel.ParentId, varModel.Name, varModel.Value, varModel.Units);
            if (rowNum < 0) { MessageBox.Show(rowNum.ToString()); }
            radGridView1.Rows[rowNum].Tag = varModel;
        }
    }
}
0
KARTHIK REDDY KAMIDI
Top achievements
Rank 1
answered on 23 Feb 2011, 08:36 PM
That definitely helped. But what surprised me was that I had that call to radGridView1.Rows.Clear inside the DeferRefresh() along with the Rows.Add(). I had written Rows.Clear() in a method of its own and was being called before the loading of variables.


     // ....
   
ClearRadGridView();
 
    using (this.radGridView1.DeferRefresh())
    {
        //radGridView1.Rows.Clear();
 
        foreach (var item in list)
        {
            var rowNum = radGridView1.Rows.Add(item.Id, item.ParentId, item.Name, item.Value, item.Units);
 
            // ....
 
 
 
 
private void ClearRadGridView()
{
    //radGridView1.BeginUpdate();
    using (radGridView1.DeferRefresh())
    {
        radGridView1.Rows.Clear();
    }
    //radGridView1.EndUpdate();
}
0
Richard Slade
Top achievements
Rank 2
answered on 23 Feb 2011, 10:03 PM
Glad that helped. Please remember to mark as answer.
For information I see you had Begin/End Update there too commented out. The Begin/EndUpdate is another way of writing DeferRefresh() (it does exactly the same thing)

Regards,
Richard
Tags
GridView
Asked by
KARTHIK REDDY KAMIDI
Top achievements
Rank 1
Answers by
Richard Slade
Top achievements
Rank 2
KARTHIK REDDY KAMIDI
Top achievements
Rank 1
Share this question
or