Click here to Skip to main content
15,881,641 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
Comments
Sunasara Imdadhusen 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/ 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.

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.
XML
<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:
XML
<ItemStyle CssClass="alignRight" />

CSS:
.alignRight{text-align:right;}
 
Share this answer
 
Comments
Ankur\m/ 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/ 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?
XML
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?
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900