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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T11:48:16+00:00 2026-06-18T11:48:16+00:00

I an using a DataGridView, which is bound to a DataTable. The DefaultCellStyle.WrapMode is

  • 0

I an using a DataGridView, which is bound to a DataTable. The DefaultCellStyle.WrapMode is set to false and everything is working just the way I want it. However, I want to use a custom CellPainting (code below), which does what is supposed to, but the WrapMode is no longer respected and longer strings are now wrapped when added to the “URL” column of DataGridView1.

  private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
   { 
       if (e.RowIndex < 0) return;

       if (e.ColumnIndex == dataGridView1.Columns["URL"].Index)
       {
           if ((e.State & DataGridViewElementStates.Selected) ==
              DataGridViewElementStates.Selected)
               return;
           e.CellStyle.WrapMode = DataGridViewTriState.False;
           Rectangle rect = new Rectangle(e.CellBounds.X, e.CellBounds.Y,
                                          e.CellBounds.Width - 1, e.CellBounds.Height - 1);

           using (System.Drawing.Drawing2D.LinearGradientBrush lgb =
             new System.Drawing.Drawing2D.LinearGradientBrush(rect, Color.White, Color.Honeydew, 0f))
           {
               e.Graphics.FillRectangle(lgb, rect);
           }

           if (e.Value == null) return;

           using (System.Drawing.Pen pen = new System.Drawing.Pen(dataGridView1.GridColor))
           {
               e.Graphics.DrawRectangle(pen, e.CellBounds.X - 1, e.CellBounds.Y - 1,
                 e.CellBounds.Width, e.CellBounds.Height);
           }

           StringFormat sf = new StringFormat();
           sf.LineAlignment = StringAlignment.Center;
           sf.Alignment = StringAlignment.Near;

           using (System.Drawing.Brush valueBrush = new SolidBrush(e.CellStyle.ForeColor))
           {
               e.Graphics.DrawString(e.Value.ToString(), e.CellStyle.Font, valueBrush, rect, sf);

           }

           e.Handled = true;
       }

     }

I have tried adding the following line:

dataGridView1.DefaultCellStyle.WrapMode = DataGridViewTriState.False;

it does not work, I have tried

e.CellStyle.WrapMode = DataGridViewTriState.False;

it does not work either.

How do I use the custom CellPainting and set the DefaultCellStyle.WrapMode to false?

  • 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-18T11:48:18+00:00Added an answer on June 18, 2026 at 11:48 am

    Instead of e.Graphics.DrawString(e.Value.ToString(), e.CellStyle.Font, valueBrush, rect, sf); try to use TextRenderer.

    var textFormatFlag = TextFormatFlags.SingleLine | TextFormatFlags.HorizontalCenter | TextFormatFlags.VerticalCenter;
    TextRenderer.DrawText(e.Graphics, e.Value.ToString(), e.CellStyle.Font, rect, e.CellStyle.ForeColor, textFormatFlag);
    

    If content of cell is wider than cell add this flag as well:

    TextFormatFlags.EndEllipsis
    

    to end it with ‘…’ 🙂

    For more information what options are available look here.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

1. I am using a DataGridView, which is bound to a dataset. There is
I have a strange problem with a DataGridView I'm using which is bound to
I have a DataGridView which is bound to a DataTable that contains a column
I have a DataTable which is bound to DataGridView . DataTable has several columns
I am developing an app in C# in which I am using the datagridview
I'm using a DataGridView in a WinForms project with the AutoSizeRowsMode set to AllCells.
Using Windows form (Vb.net & Datagridview) Checkbox is using in datagridview row. I want
I'm running into an issue using a DataGridView bound to a iBindingListView implementation (third
I'm using EntityFrameowrk 4 and WinForms. I have a DataGridView which shows 5 to
I Loaded Data From Datatable to DatagridView which look like this: SerialNumber1 | Quantity

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.