Adding Rows to non-bound RadDataGridView performance

1 Answer 89 Views
GridView
Michael
Top achievements
Rank 2
Iron
Michael asked on 06 Sep 2022, 05:58 PM
I'm populating the RadDataGridView via setting up the columns, then suppling an array of objects for each row I need to add. I can't use a bound grid as we need columns that are not actual values of the object (for example View/Edit buttons that then open the record for the user). I cannot run this in a virtual grid as we are using the grouping functionality to allow users to organize the data. Currently populating the grid takes over half the time of the process. Anyone have additional suggestion for things to try?
 public void AdvancedRowsPopulation(IList<object[]> rows, bool doReset = true)
        {
            var tmr = System.Diagnostics.Stopwatch.StartNew();
            radGridView1.BeginUpdate();
//Have to reset the columns so the object[] matches up again
            string settings = GetGridXml();
            SetGridToXML(BaseGridSettingsXML);
            DetachColumnListeners();
            tmr.Stop();
            Log.Info($"Grid to default took {tmr.ElapsedMilliseconds / 1000.0}s.");

            tmr = System.Diagnostics.Stopwatch.StartNew();
            if (doReset)
            {
                radGridView1.Rows.Clear();
                cachedRows = new List<object[]>();
                lastDataSet = new DataTable();
                foreach (var col in radGridView1.Columns) lastDataSet.Columns.Add(col.HeaderText);
            }
            for (int y=0;y<rows.Count;y++)
            {
                var row =rows[y];
                radGridView1.Rows.Add(row);
                lastDataSet.Rows.Add(row);
            }
            lblDataCount.Text = radGridView1.Rows.Count.ToString();
            tmr.Stop();
            Log.Info($"Grid Row Population took {tmr.ElapsedMilliseconds / 1000.0}s.");

            tmr = System.Diagnostics.Stopwatch.StartNew();
            BaseGridSettingsXML = GetGridXml();
//Set grid back to what the user had last seen
            SetGridToXML(settings);

            radGridView1.EndUpdate();
            tmr.Stop();
            Log.Info($"Grid to user's layout took {tmr.ElapsedMilliseconds / 1000.0}s.");
        }

1 Answer, 1 is accepted

Sort by
0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 07 Sep 2022, 08:53 AM

Hello, Michael, 

Please have in mind that you can set the RadGridView.DataSource property to the desired collection and then add more columns that are not part of the source collection. Thus, you can also achieve the View/Edit buttons you need. A sample approach is demonstrated here:

https://docs.telerik.com/devtools/winforms/knowledge-base/edit-a-cell-in-gridview-with-a-dialog

As to the unbound mode, note that each add row operation refreshes the view. Hence, the more add operations you have, the more operations of refreshing the grid will be executed. For such cases, to optimize the performance, you can wrap the rows adding in BeginUpdate/EndUpdate block:

            this.radGridView1.BeginUpdate();
            //add unbound rows
            this.radGridView1.EndUpdate();

I hope this information helps. If you need any further assistance please don't hesitate to contact me. 

Regards,
Dess | Tech Support Engineer, Principal
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

Michael
Top achievements
Rank 2
Iron
commented on 07 Sep 2022, 12:36 PM

I will have to play with using a bounded list. My one concern with that approach is we have an image column that shows an icon that correlates to the record status.

Short of making a wrapper class to hold the image as a property (which I guess I already do in a way when I convert the class to an object[]), is there a simple way to have a cell's value be dynamic? 

Dess | Tech Support Engineer, Principal
Telerik team
commented on 07 Sep 2022, 01:20 PM

RadGridView offers the CellFormatting event which is suitable for assigning an image according to another cell's value.  

        public RadForm1()
        {
            InitializeComponent();

            DataTable dt = new DataTable();
            dt.Columns.Add("Id", typeof(int));
            dt.Columns.Add("Title", typeof(string));
            dt.Columns.Add("Status", typeof(Status));

            for (int i = 0; i < 20; i++)
            {
                dt.Rows.Add(i, "Title" + i, 0);
            }

            this.radGridView1.CellFormatting += radGridView1_CellFormatting;
            this.radGridView1.DataSource = dt;
            this.radGridView1.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill;
        }

        private void radGridView1_CellFormatting(object sender, CellFormattingEventArgs e)
        {
            if (e.Column.Name == "Status" && e.CellElement.Value != null)
            {
                Status status = (Status)e.CellElement.Value;
                e.CellElement.DrawText = false;
                e.CellElement.DrawImage = true;
                e.CellElement.ImageLayout = ImageLayout.Zoom;
                switch (status)
                {
                    case Status.New:
                        e.CellElement.Image = Properties.Resources._new;
                        break;
                    case Status.InProgress:
                        e.CellElement.Image = Properties.Resources.inprogress;
                        break;
                    case Status.Done:
                        e.CellElement.Image = Properties.Resources.done;
                        break;
                    default:
                        break;
                }
            }
            else
            { 
                e.CellElement.ResetValue(LightVisualElement.ImageProperty, ValueResetFlags.Local);
                e.CellElement.ResetValue(LightVisualElement.DrawImageProperty, ValueResetFlags.Local);
                e.CellElement.ResetValue(LightVisualElement.ImageLayoutProperty, ValueResetFlags.Local);
                e.CellElement.ResetValue(LightVisualElement.DrawTextProperty, ValueResetFlags.Local);
            }
        }

        public enum Status
        { 
            New = 0,
            InProgress = 1,
            Done = 2
        }

Tags
GridView
Asked by
Michael
Top achievements
Rank 2
Iron
Answers by
Dess | Tech Support Engineer, Principal
Telerik team
Share this question
or