Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8736161
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T10:14:32+00:00 2026-06-13T10:14:32+00:00

I have a DataGrid in Compact Framework I need to fill out with tens

  • 0

I have a DataGrid in Compact Framework I need to fill out with tens of thousands of records, this is proving to be an issue as I’m running out of memory when I try that.

Is there a way to dynamically only show and load up rows the user is looking at rather than dumping all the rows in one go into the grid?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T10:14:33+00:00Added an answer on June 13, 2026 at 10:14 am

    The below code will automatically resize column widths for a datagrid whose datasource is an array.

        public static void AutosizeColumnsWithArray(DataGrid grid, PaintEventArgs e)
        {
            Font dataFont = grid.Font;
            Font headerFont = GetHeaderFont(dataFont);
            IList list = null;
            if (grid.DataSource is IList)
            {
                list = (IList) grid.DataSource;
            }
            else if (grid.DataSource is IListSource)
            {
                list = ((IListSource) grid.DataSource).GetList();
            }
    
            if (list == null || list.Count < 0)
            {
                return;
            }
    
            if (grid.TableStyles.Count == 0)
            {
                return;
            }
    
            var pdc = TypeDescriptor.GetProperties(list[0]);
            var dataGridTableStyle = new DataGridTableStyle();
            dataGridTableStyle.MappingName = GetMappingName(list);
            int totalWidth = 0;
    
            for (int i = 0; i < pdc.Count; i++)
            {
                var columnName = pdc[i].Name;
    
                bool validColumn = grid.TableStyles[0].GridColumnStyles.Contains(columnName);
                if (!validColumn)
                {
                    continue;
                }
    
                var gridColumnStyle = grid.TableStyles[0].GridColumnStyles[columnName];
                var indexToEdit = grid.TableStyles[0].GridColumnStyles.IndexOf(gridColumnStyle);
    
                var headerText = grid.TableStyles[0].GridColumnStyles[columnName].HeaderText;
                var maxSize = e.Graphics.MeasureString(headerText, headerFont);
                int maxLength = headerText.Length;
    
                int rowCount = 0;
                const int maxRowsToCompare = 200;
                foreach (object o in list)
                {
                    if (rowCount == maxRowsToCompare)
                    {
                        break;
                    }
                    object result = pdc[i].GetValue(o);
    
                    string value = result == null ? string.Empty : result.ToString().Trim();
    
                    if (ValueCannotBeLongest(value, maxLength))
                    {
                        rowCount++;
                        continue;
                    }
    
                    SizeF size = e.Graphics.MeasureString(value, dataFont);
                    if (size.Width > maxSize.Width)
                    {
                        maxSize = size;
                        maxLength = value.Length;
                    }
                    rowCount++;
                }
                var newWidth = (int) (maxSize.Width + 5);
                grid.TableStyles[0].GridColumnStyles[indexToEdit].Width = newWidth;
                totalWidth += newWidth;
            }
        }
    
        private static bool ValueCannotBeLongest(string value, int maxLength)
        {
            return (value.Length == 0) || (value.Length + 3 < maxLength);
        }
    
        private static string GetMappingName(IList list)
        {
            string result;
    
            if (list is ITypedList)
            {
                result = ((ITypedList) list).GetListName(null);
            }
            else
            {
                result = list.GetType().Name;
            }
    
            return result;
        }
    
        private static Font GetHeaderFont(Font dataFont)
        {
            string FontName = dataFont.Name;
            float FontSize = dataFont.Size;
            return new Font(FontName, FontSize, FontStyle.Bold);
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a datagrid in .net compact framework C# and I set its datasource
I have datagrid. the source for this datagrid is a url that returns n
I have a DataGrid (dataGrid1) where records can be added and deleted. Based on
I have datagrid and inside this grid have template field. I add Header text
I have DataGrid that connects to REST service. I need to pass some custom
i have datagrid in my project which i bind my attributes collection like this:
i have a datagrid bound to a property. In this grid i have columns
I'm trying to use a System.Windows.Forms.DataGrid control in my Compact Framework 3.5 Window Mobile
Quick question regarding the Compact Framework DataGrid. How can I scroll a particular row
I have a datagrid getting its data from the database. This datagrid has a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.