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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T03:16:11+00:00 2026-05-18T03:16:11+00:00

Ok I am pretty ashamed I am posting for help with this, but here

  • 0

Ok I am pretty ashamed I am posting for help with this, but here you go. I just need to display some data in 3 evenly adjusted columns in dropdownlist.

Here is the code to adjust the padding of each string based on the string with max length.
Second column looks ok but the last one is off by the offset of the difference of max length – string length. I cant figure out why…

 List<Data> list = new List<Data>();

            Data d = new Data() { Data1 = "328989892787", Data2 = "MNJK", Data3="23" };
            Data b = new Data() { Data1 = "343567", Data2= "HJKLLL", Data3="2345" };
            Data g = new Data() { Data1 = "64737", Data2="UI", Data3="234" };
            Data f = new Data() { Data1 = "878437878223245", Data2="", Data3="45653" };
            Data a = new Data() { Data1 = "234", Data2 = "DataMe", Data3="1"};

            list.Add(d);
            list.Add(b);
            list.Add(g);
            list.Add(f);
            list.Add(a);

            var sorted1 = list.OrderByDescending(q => q.Data1.Length).ToList();
            var sorted2 = list.OrderByDescending(s => s.Data2.Length).ToList();
            var sorted3 = list.OrderByDescending(r => r.Data3.Length).ToList();

            int maxd1 = sorted1[0].Data1.Length;
            int maxd2 = sorted2[0].Data2.Length;
            int maxd3 = sorted3[0].Data3.Length;

            ListItem item = null;
            foreach (var dd in list) 
            {

                string result = string.Format("{0}{1}{2}",
                    dd.Data1.PadRight(40 + (maxd1 - dd.Data1.Length), '\u00A0'),
                    dd.Data2.PadRight(50 +(maxd2 - dd.Data2.Length), '\u00A0'),
                    dd.Data3);

                this.DropDownList1.Items.Add(new ListItem(result, dd.Data1));

            }
  • 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-05-18T03:16:11+00:00Added an answer on May 18, 2026 at 3:16 am

    The font you use for the dropdown is the problem here. Your code assumes that every character in the strings will have the same width. This is only true for fixed-pitched fonts like Courier New or Consolas. The 2nd column still works because the 1st column only contains strings with digits. Many fonts, but not all, give digits the same width as a space. It goes wrong for the 3rd column because the 2nd one contains letters. An M needs a lot more space than an I.

    Change the font. Some controls support using a tab to align text, I can’t tell from your tags if that would apply.

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

Sidebar

Related Questions

Pretty sure there is an easy answer to this, but just can't find the
Pretty much the same as this question. But I can't seem to get this
Pretty straightforward: I'm looking to do the same as this but in winforms. Everything
I ashamed and frustrated by this question. I'm writing what should be a pretty
Pretty new here. But before I ask my question, I'm not looking for teh
Pretty simple idea, but I'm not sure how to do this. I'd like to
Pretty noobish question, and I'm probably thinking about this wrong, but... Is there a
Pretty sure I know the answer to this one (thinking no), but could one
Pretty straightforward, but I just want to know which is faster. I think simply
pretty simple question I have here, but I couldn't find the answer: Assume I

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.