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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T11:51:53+00:00 2026-05-25T11:51:53+00:00

Can anyone explain this one to me. I’m trying to set the sizes of

  • 0

Can anyone explain this one to me. I’m trying to set the sizes of a GridView.

int totalWidth = 550;
int subWidth = totalWidth / _module.Values.Count;

with 3 modules this comes to 183, and with 2 it’s 275.

Later in the code I do a:
myGrid.HeaderStyle.HorizontalAlign = HorizontalAlign.Center;
myGrid.HeaderStyle.Width = subWidth;

Now I would have assumed the width of the columns would be 183px with 3 modules, or 275 with 2. Actual sizes are: 224 + 153 + 172 = 549. The headertext of column 1 is long, but if I force it to be 183px, why does it insist of being 224? Text is column 2 is short, column 3 medio long. There is still plenty of space for all 3 headers to be squeezed a bit.

Just to be clear – I’m also doing a:
myGrid.RowStyle.HorizontalAlign = HorizontalAlign.Center;
myGrid.RowStyle.Width = subWidth;

I see the same wrong sizes in both Firefox and Chrome. I cannot get it into my stupid head how I can set a width and the column decides on another value despite my intentions.

Method in question:

private void CreateGridView()
{
GridView myGrid = new GridView();
int totalWidth = 550;
int subWidth = totalWidth / _module.Values.Count;

    #region DataTable

    DataTable dataTable = new DataTable();

    string[] dataArray = new string[_module.Values.Count];

    for (int i = 0; i < _module.Values.Count; i++)
    {
        dataArray[i] = _module.Values[i].Value.ToString("n0"); ;
    }

    for (int i = 0; i < _module.Values.Count; i++)
    {
        dataTable.Columns.Add(_module.Values[i].Text.ToString());
    }

    dataTable.Rows.Add(dataArray);

    myGrid.DataSource = dataTable;

    #endregion

    myGrid.Width = totalWidth;

    myGrid.GridLines = GridLines.None;

    myGrid.HeaderStyle.HorizontalAlign = HorizontalAlign.Center;
    myGrid.HeaderStyle.Width = subWidth;

    myGrid.RowStyle.HorizontalAlign = HorizontalAlign.Center;
    myGrid.RowStyle.Width = subWidth;

    myGrid.DataBind();

    Controls.Add(myGrid);
}
  • 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-25T11:51:53+00:00Added an answer on May 25, 2026 at 11:51 am

    Why not set CssClass on the GridView (as well as other children of the GridView such as HeaderStyle, RowStyle and so on) and set the width on that, rather than having your UI/design elements inside of compiled code?

    I would create a stylesheet, or add to an existing stylesheet with a class declaration:

    .myStyle {
        width: 550px;
    }
    
    .myStyle2Modules {
         width: 750px;
    }
    
    .myStyleLotsOfModules {
         width: 1000px;
    }
    

    and then reference the class. You can then tweak the UI positions and widths on the fly by editing the CSS. You can programatically choose the CSS class in the code, but the actual definitions of those styles are in the stylesheet.

    Let your code handle the functionality, nuts and bolts and implementation – let the stylesheet handle all the design elements such as colours, fonts, widths, borders and so on

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

Sidebar

Related Questions

Can anyone explain this to me. I'm trying to inject a CSS file onto
Hi can anyone explain this to me: On one page I have: setcookie(cookiename,$cookiedata,time()+(84600*30*24)); setcookie(cookiename2,$cookie2data,
Can anyone explain this? Project.includes([:user, :company]) This executes 3 queries, one to fetch projects,
Google-fu is failing me on this one. Can anyone briefly explain what the following
Can anyone explain a parse error like this one: Method 'get_EnableCdn' in type 'System.Web.UI.ScriptManager'
Can anyone explain this code to me? Whats the meaning and how to use
Can anyone explain why this code: {% form_theme form _self %} {% block avo_gallery_upload_widget
Can anyone explain why this code with the given routes returns the first route?
Can anyone explain why this query returns an empty result. SELECT * FROM (`bookmarks`)
Can anyone explain me why this throws a findbug warning: if (m != null

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.