473,569 Members | 2,692 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 3627
Hi,
you'd need to set autogeneratecol umns 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" AutoGenerateCol umns="False" runat="server">
<Columns>
<asp:BoundColum n DataField="some column" Header-Style-Width="60" />
etc...

Jon

"VB Programmer" <gr*********@ go-intech.com> wrote in message
news:Oz******** ******@tk2msftn gp13.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*******@NOCR APTHANKStiscali .co.uk> wrote in message
news:%2******** **********@TK2M SFTNGP12.phx.gb l...
Hi,
you'd need to set autogeneratecol umns 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" AutoGenerateCol umns="False" runat="server">
<Columns>
<asp:BoundColum n DataField="some column" Header-Style-Width="60" />
etc...

Jon

"VB Programmer" <gr*********@ go-intech.com> wrote in message
news:Oz******** ******@tk2msftn gp13.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,h elvetica,sans-serif;
border: 1px solid black; }
then set a couple of styles for columns
..theHeader{wid th:20%; }
..wideColumn{ width:60%;}
now build up the grid
<asp:DataGrid ID="TheGrid" Runat="server" AutoGenerateCol umns="False"
CssClass="theTa ble" GridLines="None ">
<Columns>
<asp:BoundColum n DataField="fiel d1" HeaderStyle-CssClass="wideC olumn"
HeaderText="Tex t" />
<asp:BoundColum n DataField="fiel d2" HeaderStyle-CssClass="theHe ader"
HeaderText="Tex t1" />
<asp:BoundColum n DataField="fiel d3" HeaderStyle-CssClass="theHe ader"
HeaderText="Tex t2" />
</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******** ******@TK2MSFTN GP12.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*******@NOCR APTHANKStiscali .co.uk> wrote in message
news:%2******** **********@TK2M SFTNGP12.phx.gb l...
Hi,
you'd need to set autogeneratecol umns 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" AutoGenerateCol umns="False" runat="server">
<Columns>
<asp:BoundColum n DataField="some column" Header-Style-Width="60" />
etc...

Jon

"VB Programmer" <gr*********@ go-intech.com> wrote in message
news:Oz******** ******@tk2msftn gp13.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.C olumns.Count - 1

'Capturing the column heading length
iColNameLen = Len(DgTableEdit or.Columns.Item (x).DataField)
'temp variable to store max length
TmpNewWidth = iColNameLen
'Get length of text
DGColLen = Len(RTrim(DgTab leEditor.Column s.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.C olumns.Item(y). Width = ColNewWidth(y) * 125
Next
End Sub

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

"John" <jo**********@i nfineon.com> wrote in message
news:3b******** *************** ***@posting.goo gle.com...
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.C olumns.Count - 1

'Capturing the column heading length
iColNameLen = Len(DgTableEdit or.Columns.Item (x).DataField)
'temp variable to store max length
TmpNewWidth = iColNameLen
'Get length of text
DGColLen = Len(RTrim(DgTab leEditor.Column s.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.C olumns.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
3242
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)); DataTableAddress.Columns.Add("Col3", typeof(string)); I'd like to add to resize them without using a datacolumn because there's over 30 columns and because...
9
3935
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 selection column on the left. I have the widths of the columns. What other values do I need to include in the DataGrid width? Thanx,
3
4233
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 of a subclassed DataGridTextBoxColum dos not seem like a practical way to do it. I have subclassed a DataGrid and overridden the OnPaint as...
1
2143
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 code in the "ItemDataBound" -- which I belive should allow me to alter the width of the cells, however it does not work. Dim unitWidth As Unit...
1
2213
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
6384
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
4673
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 button will be dynamic and always show up on the same Y-axis position as the selected row. So, how do I get back the X, Y co-ordinates of a...
1
2050
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 the framework is only wide enough to accomodate the data. The data is packed so closly together it is hard to read. I have tried changing this...
2
6572
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 in the child datagrid and then go to the combobox and choose another vendor. When the new vendor is loaded nothing shows in the datagrid but the...
0
7698
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7612
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7924
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8122
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7673
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6284
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5513
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
1
2113
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
937
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.