473,408 Members | 2,832 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

Question: Changing datagrid's column width

I have a simple datagrid on a webform. (I'm using VB.NET.)

In the page load I get some data, set the .DataSource property of the dg,
then do a .DataBind. The columns are automatically created.

Question: How can I dynamically change the width's of these columns?

Thanks in advance.
Nov 18 '05 #1
5 3617
Hi,
you'd need to set autogeneratecolumns to false and then add a column for
each you want, you'll then be able to specify the width for each column -
either directly or throught CSS, eg
<asp:datagrid id="whatever" AutoGenerateColumns="False" runat="server">
<Columns>
<asp:BoundColumn DataField="somecolumn" Header-Style-Width="60" />
etc...

Jon

"VB Programmer" <gr*********@go-intech.com> wrote in message
news:Oz**************@tk2msftngp13.phx.gbl...
I have a simple datagrid on a webform. (I'm using VB.NET.)

In the page load I get some data, set the .DataSource property of the dg,
then do a .DataBind. The columns are automatically created.

Question: How can I dynamically change the width's of these columns?

Thanks in advance.

Nov 18 '05 #2
As you suggested I set the autogenerate to false and added my columns. I
went to the property builder and specified a width for each column (in
Properties/Format/Columns). I checked in HTML and it added the Width
variable. But, when I run the page it seems to ignore the width.
Everything is really snug and tight. Also, when I go into edit mode (I have
an Edit, Update, Cancel button), all the columns get equally (very) wide.
I'm not sure how this width is set, but I would like to set this one as
well.

Any ideas?

"Jon Spivey" <jo*******@NOCRAPTHANKStiscali.co.uk> wrote in message
news:%2******************@TK2MSFTNGP12.phx.gbl...
Hi,
you'd need to set autogeneratecolumns to false and then add a column for
each you want, you'll then be able to specify the width for each column -
either directly or throught CSS, eg
<asp:datagrid id="whatever" AutoGenerateColumns="False" runat="server">
<Columns>
<asp:BoundColumn DataField="somecolumn" Header-Style-Width="60" />
etc...

Jon

"VB Programmer" <gr*********@go-intech.com> wrote in message
news:Oz**************@tk2msftngp13.phx.gbl...
I have a simple datagrid on a webform. (I'm using VB.NET.)

In the page load I get some data, set the .DataSource property of the dg, then do a .DataBind. The columns are automatically created.

Question: How can I dynamically change the width's of these columns?

Thanks in advance.


Nov 18 '05 #3
Hi,
the way I tend to do this is to set a CSS class for the tables and cells -
this way you dont end up with inline styles all over the place. Start by
setting a style for the table itself, eg
..theTable{
width:80%;
font:12px verdana,arial,helvetica,sans-serif;
border: 1px solid black; }
then set a couple of styles for columns
..theHeader{width:20%; }
..wideColumn{ width:60%;}
now build up the grid
<asp:DataGrid ID="TheGrid" Runat="server" AutoGenerateColumns="False"
CssClass="theTable" GridLines="None">
<Columns>
<asp:BoundColumn DataField="field1" HeaderStyle-CssClass="wideColumn"
HeaderText="Text" />
<asp:BoundColumn DataField="field2" HeaderStyle-CssClass="theHeader"
HeaderText="Text1" />
<asp:BoundColumn DataField="field3" HeaderStyle-CssClass="theHeader"
HeaderText="Text2" />
</Columns>

You can also set seperate classes for the table rows if you want - although
obviously you only need to set width for the headers.

Jon
"VB Programmer" <gr*********@go-intech.com> wrote in message
news:O6**************@TK2MSFTNGP12.phx.gbl...
As you suggested I set the autogenerate to false and added my columns. I
went to the property builder and specified a width for each column (in
Properties/Format/Columns). I checked in HTML and it added the Width
variable. But, when I run the page it seems to ignore the width.
Everything is really snug and tight. Also, when I go into edit mode (I have an Edit, Update, Cancel button), all the columns get equally (very) wide.
I'm not sure how this width is set, but I would like to set this one as
well.

Any ideas?

"Jon Spivey" <jo*******@NOCRAPTHANKStiscali.co.uk> wrote in message
news:%2******************@TK2MSFTNGP12.phx.gbl...
Hi,
you'd need to set autogeneratecolumns to false and then add a column for
each you want, you'll then be able to specify the width for each column -
either directly or throught CSS, eg
<asp:datagrid id="whatever" AutoGenerateColumns="False" runat="server">
<Columns>
<asp:BoundColumn DataField="somecolumn" Header-Style-Width="60" />
etc...

Jon

"VB Programmer" <gr*********@go-intech.com> wrote in message
news:Oz**************@tk2msftngp13.phx.gbl...
I have a simple datagrid on a webform. (I'm using VB.NET.)

In the page load I get some data, set the .DataSource property of the

dg, then do a .DataBind. The columns are automatically created.

Question: How can I dynamically change the width's of these columns?

Thanks in advance.



Nov 18 '05 #4
I messed with this for a while. It was not very friendly. Sorry for
the sloppy code but it works.
Dim ColNameLen As Integer, x As Integer, DGColLen As Integer,
ColNewWidth(50) As Integer

'Looping through the number of cols in the grid
For x = 0 To DgTableEditor.Columns.Count - 1

'Capturing the column heading length
iColNameLen = Len(DgTableEditor.Columns.Item(x).DataField)
'temp variable to store max length
TmpNewWidth = iColNameLen
'Get length of text
DGColLen = Len(RTrim(DgTableEditor.Columns.Item(x).Text))
'Compare text length to col name length
If DGColLen > TmpNewWidth Then TmpNewWidth = DGColLen
'Put largest amount in array with corresponding column as
subscript
ColNewWidth(x) = TmpNewWidth

Next 'loop

'Loop through columns and set width accordingly.
For y = 0 To x - 1
DgTableEditor.Columns.Item(y).Width = ColNewWidth(y) * 125
Next
End Sub

Regards,
John W.
Nov 18 '05 #5
Thanks everyone!

"John" <jo**********@infineon.com> wrote in message
news:3b**************************@posting.google.c om...
I messed with this for a while. It was not very friendly. Sorry for
the sloppy code but it works.
Dim ColNameLen As Integer, x As Integer, DGColLen As Integer,
ColNewWidth(50) As Integer

'Looping through the number of cols in the grid
For x = 0 To DgTableEditor.Columns.Count - 1

'Capturing the column heading length
iColNameLen = Len(DgTableEditor.Columns.Item(x).DataField)
'temp variable to store max length
TmpNewWidth = iColNameLen
'Get length of text
DGColLen = Len(RTrim(DgTableEditor.Columns.Item(x).Text))
'Compare text length to col name length
If DGColLen > TmpNewWidth Then TmpNewWidth = DGColLen
'Put largest amount in array with corresponding column as
subscript
ColNewWidth(x) = TmpNewWidth

Next 'loop

'Loop through columns and set width accordingly.
For y = 0 To x - 1
DgTableEditor.Columns.Item(y).Width = ColNewWidth(y) * 125
Next
End Sub

Regards,
John W.

Nov 18 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Amos | last post by:
Can I add a column to the datagrid and also resize it? I add them like this: DataTableAddress.Columns.Add("Col1", typeof(string)); DataTableAddress.Columns.Add("Col2", typeof(string));...
9
by: web1110 | last post by:
Hi y'all, I have resized the columns in a DataGrid and I want to set the width of the DataGrid to fit the columns. Just summing the column widths is too short due to the grid and gray row...
3
by: Richard | last post by:
I have a requirement to put a GDI style circle or rectangle border around the selected row of a datagrid/ It will overlap into the row above and below the selected row. Doing this in a the OnPaint...
1
by: martin | last post by:
Hi, I have a datagrid that contains 3 colums. This is rendered to the page fine, except that I would like to be able to control the width of each table cell of the datagrid I have the following...
1
by: VB Programmer | last post by:
I have a datagrid which is bound to a datatable that I create on the fly. How do I set the width of each column via code? Ex: Column 1 is always 100, Column 2 is always 250, etc... Thanks!
2
by: CSL | last post by:
I am using the DataGrid in a Windows Application, how can I adjust the widths of each column individually.
3
by: Aziz | last post by:
1. I have a shopping basket DataGrid with a list of products. What I want to do is when the user clicks on a row, a button will become visible/be created that allows user to edit the quantity. The...
1
by: Veeves | last post by:
I would like to change the width of a datagrid column at run time. I have noticed when creating a datagrid, if the header text is shorter than the data in the column, then the column width set by...
2
by: =?Utf-8?B?Y3JlYXZlczA2MjI=?= | last post by:
I have a nested datagrid in a xaml file, the parent datagrid loads the vendor information and the details loads the documents for that vendor in a datagrid. Everything is working fine until I click...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.