Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: VB8.0 ASP.NET
I have a datagrid which is linked to a stored procedure which retrieves records from a underlying database.
 
There are many columns and I want to size the width of each column to the max. length of the largest value in each column.
 
Also if the data is numeric I want to left justify within the cell.
 
How do I achieve this in a web form with ASP.NET 2 and vb.Net?
Posted 25-Oct-10 2:58am
AlHal2378
Comments
Sunasara Imdadhusen at 25-Oct-10 8:22am
   
You will be able to do after getting all records from database.
traverse all the rows one by one and find max length of each string.
after finding max you have to set Gridview column width accordingly.
Ankur\m/ at 25-Oct-10 9:11am
   
Use 'Add Comment' below a user's answer to reply to him because that notifies the user about your response, an answer doesn't.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

There are many columns and I want to size the width of each column to the max. length of the largest value in each column.
 

For each column, that shouldn't wrap, set the ItemStyle wrap property to false.
<ItemStyle Wrap="False" />
 
Also if the data is numeric I want to left justify within the cell.
 

Again, you can use ItemStyle to style the column.
Do something like this:
<ItemStyle CssClass="alignRight" />
CSS:
.alignRight{text-align:right;}
  Permalink  
Comments
Ankur\m/ at 25-Oct-10 9:09am
   
[moved from answer]
AlHal2 wrote:
Still no good. Perhaps the problem is with the data itself. It's a date time stamp eg 2010-07-26 11:01:52.000
Ankur\m/ at 25-Oct-10 9:12am
   
It should work in any case. Check the generated html of the page. Does it have 'nowrap' set for the required columns?
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Still no good. Perhaps the problem is with the data itself. It's a date time stamp eg 2010-07-26 11:01:52.000. Actually that's wrong as varchar variables are also wrapping. Here is the code that generates the grid.
 

 Collapse | Copy Code
<asp:GridView ID="GridView1" runat="server" AllowSorting="True"  Font-Names="Arial" Font-Size="Small" AllowPaging="True" ItemStyle Wrap="False"
>
          <RowStyle BackColor="White" Wrap="True" />
How do I set nowrap for selected columns when the data comes from a stored procedure?
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 7,903
1 Sergey Alexandrovich Kryukov 7,127
2 DamithSL 5,604
3 Manas Bhardwaj 4,986
4 Maciej Los 4,820


Advertise | Privacy | Mobile
Web03 | 2.8.1411023.1 | Last Updated 25 Oct 2010
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100