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 9162509
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T14:13:40+00:00 2026-06-17T14:13:40+00:00

This question is asked here many times but their answers are not working for

  • 0

This question is asked here many times but their answers are not working for me.

I am using C# DataGridView to show the data in tabular form. I have some 8/9 columns and around 25 to 30 rows. For each column, I am setting customized width (Some percentage of total screen width and it is different for each column). To achieve it I have set following property

    view1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.None;

Also, since my application is such that I can’t show all the rows at same time but the no. of rows defined by user at a time and for that I am using scrollindex property in timer to scroll view. Also I have customized row height (Some percentage of total screen height). So to do not show all the rows at same time I am using following property

    view1.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.None;

For wrapping I am using

    view1.DefaultCellStyle.WrapMode = DataGridViewTriState.True;

But the above property wraps the only words which have space in between but not the single long word.

So how to wrap the single long word with above row and column size properties? In lots of places I am getting advice to use

    view1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.DisplayedCells;
    view1.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.DisplayedCells;

But whenever I use anything other than ‘none’ in AutoSizeColumnsMode my column’s customized width gets changed and if I use anything other than ‘none’ in AutoSizeRowsMode , it shows all the rows at a time and my row’s customized height gets changed.

Anybody knows how to solve this problem?

Note: It is a desktop application (Windows Form Application).

  • 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-17T14:13:42+00:00Added an answer on June 17, 2026 at 2:13 pm

    Use DataGridView CellPainting event.
    Sample code:

     public Form1()
        {
            InitializeComponent();
        }
    
        private void Form1_Load(object sender, EventArgs e)
        {
            dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.None;
            dataGridView1.DefaultCellStyle.WrapMode = DataGridViewTriState.True;
    
            //dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.DisplayedCells;
            //dataGridView1.DefaultCellStyle.WrapMode = DataGridViewTriState.True;
            //dataGridView1.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.DisplayedCells;
    
            foreach (DataGridViewRow row in dataGridView1.Rows)
            {
                row.Cells[0].Value = "CELL123456789012345679801234567890";
                row.Cells[1].Value = "CELL 1";
                row.Cells[2].Value = "CELL 2";
                row.Cells[3].Value = "CELL 3";
                row.Cells[4].Value = "CELL 4";
                row.Cells[5].Value = "CELL 5";
            }
    
        }
    
        private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
        {
            if (e.Value == null)
                return;
            var s = e.Graphics.MeasureString(e.Value.ToString(), dataGridView1.Font);
            if (s.Width > dataGridView1.Columns[e.ColumnIndex].Width)
            {
                using (
          Brush gridBrush = new SolidBrush(this.dataGridView1.GridColor),
          backColorBrush = new SolidBrush(e.CellStyle.BackColor))
                {
                    e.Graphics.FillRectangle(backColorBrush, e.CellBounds);
                    e.Graphics.DrawString(e.Value.ToString(), dataGridView1.Font, Brushes.Black, e.CellBounds, StringFormat.GenericDefault);
                    dataGridView1.Rows[e.RowIndex].Height = (int)(s.Height * Math.Ceiling(s.Width / dataGridView1.Columns[e.ColumnIndex].Width));
                    e.Handled = true;
                }
            }
        }
    

    Improved code:

    private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
        {
            if (e.Value == null)
                return;
    
            var s = e.Graphics.MeasureString(e.Value.ToString(), dataGridView1.Font);
    
            if (s.Width > dataGridView1.Columns[e.ColumnIndex].Width)
            {
                using (Brush gridBrush = new SolidBrush(this.dataGridView1.GridColor), backColorBrush = new SolidBrush(e.CellStyle.BackColor))
                    {
                        using (Pen gridLinePen = new Pen(gridBrush))
                        {
                            e.Graphics.FillRectangle(backColorBrush, e.CellBounds);
                            e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left, e.CellBounds.Bottom - 1, e.CellBounds.Right, e.CellBounds.Bottom - 1);
                            e.Graphics.DrawLine(gridLinePen, e.CellBounds.Right - 1, e.CellBounds.Top, e.CellBounds.Right - 1, e.CellBounds.Bottom - 1);
    
                            e.Graphics.DrawString(e.Value.ToString(), dataGridView1.Font, Brushes.Black, e.CellBounds, StringFormat.GenericDefault);
    
                            dataGridView1.Rows[e.RowIndex].Height = (int)(s.Height * Math.Ceiling(s.Width / dataGridView1.Columns[e.ColumnIndex].Width));
    
                            e.Handled = true;
                        }
    
                    }
            }
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know this question was asked here many times before but I am still
This question has been asked many times here on StackOverflow, but I don't see
I know this question was asked many times, but still my problem is not
I know this question has been asked many times here, but no answer has
I know this question has been asked many times on here but I have
I know this question has probably been asked so many times here, but I
I know this question has been asked many times, but here are my specific
I know this question has been asked many times, but I'm not able to
I am afraid this question is not supposed to be asked here but I
I know this question has been asked many times, but I couldn't manage to

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.