473,480 Members | 2,057 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Setting column widths in a datagrid.


I'm trying to set the column widths for a datagrid. You'd think it
would be easy. I looked it up in google and the first thing I found
looked promising:

datagrid1.columns(0).width = 2000
datagrid1.columns(1).width = 1000

Except that there is no such things as a columns collection in a
datagrid.

Another person suggested right clicking on the datagrid, clicking
properties, and then clicking Layout. That sounded promising except
that there is no Layout option.

My .NET book says to right click the datagrid and then select Property
Builder. Cool! Oops! There is no Property Builder option.

The only other thing I found is to somehow add a datagridtextboxcolumn
object to a datagridtablestyles object and then add that to my datagrid
and that is supposed to somehow set the column width. My first attempts
had no effect on changing the columns but at least I didn't get any
syntax errors. But I can't imagine it would be this complicated to
simply set the column width.

Help!!!!

Nov 21 '05 #1
6 7073
<cr***@hotmail.com> schrieb:
I'm trying to set the column widths for a datagrid.


<URL:http://msdn.microsoft.com/library/en-us/dv_vstechart/html/vbtchformattingwindowsformsdatagridvisualbasicprim er.asp>
-> "Setting Column Width"

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
Nov 21 '05 #2
Sounds to me like youre not using VS.NET... Because there is most
certainly a Property Builder option when you right click the grid... In
any case, it wont let you set column widths up on an individual basis,
so you can do this in 2 ways (both of which will occur in the DataGrid's
ItemDataBound event:

Here's the basics (watch for wordwrap):

Private Sub DataGrid1_ItemDataBound(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.DataGridItemEventArgs) Handles
DataGrid1.ItemDataBound
If e.Item.ItemType = ListItemType.AlternatingItem Or
e.Item.ItemType = ListItemType.Item Then
For cellCounter As Integer = 0 To e.Item.Cells.Count - 1
' either use the Attributes collection
e.Item.Cells(cellCounter).Attributes.Add("width"," 30px")

' or specify the width directly,
e.Item.Cells(cellCounter).width = Unit.Pixels(30)
Next
End If
End Sub

HTH
cr***@hotmail.com wrote:
I'm trying to set the column widths for a datagrid. You'd think it
would be easy. I looked it up in google and the first thing I found
looked promising:

datagrid1.columns(0).width = 2000
datagrid1.columns(1).width = 1000

Except that there is no such things as a columns collection in a
datagrid.

Another person suggested right clicking on the datagrid, clicking
properties, and then clicking Layout. That sounded promising except
that there is no Layout option.

My .NET book says to right click the datagrid and then select Property
Builder. Cool! Oops! There is no Property Builder option.

The only other thing I found is to somehow add a datagridtextboxcolumn
object to a datagridtablestyles object and then add that to my datagrid
and that is supposed to somehow set the column width. My first attempts
had no effect on changing the columns but at least I didn't get any
syntax errors. But I can't imagine it would be this complicated to
simply set the column width.

Help!!!!

Nov 21 '05 #3
whoops - I took it for granted that you were putting this together in
ASP.NET. Never mind.

cr***@hotmail.com wrote:
I'm trying to set the column widths for a datagrid. You'd think it
would be easy. I looked it up in google and the first thing I found
looked promising:

datagrid1.columns(0).width = 2000
datagrid1.columns(1).width = 1000

Except that there is no such things as a columns collection in a
datagrid.

Another person suggested right clicking on the datagrid, clicking
properties, and then clicking Layout. That sounded promising except
that there is no Layout option.

My .NET book says to right click the datagrid and then select Property
Builder. Cool! Oops! There is no Property Builder option.

The only other thing I found is to somehow add a datagridtextboxcolumn
object to a datagridtablestyles object and then add that to my datagrid
and that is supposed to somehow set the column width. My first attempts
had no effect on changing the columns but at least I didn't get any
syntax errors. But I can't imagine it would be this complicated to
simply set the column width.

Help!!!!

Nov 21 '05 #4
The way I size my columns is through adding a bunch of
DataGridTextBoxColumn's to a DataGridTableStyle. I query about 10
items from my database. So I create 10 DataGridTextBoxColumn's and add
them all the the tablestyle, then set the datagrid's tablestyle to the
one i created. See:

Dim ts1 As New DataGridTableStyle
ts1.AlternatingBackColor = Color.LightCyan
ts1.MappingName = "MAPPING_NAME"
ts1.SelectionBackColor = Color.SteelBlue
' etc..

Dim mydatacol As New DataGridTextBoxColumn
mydatacol.MappingName = "Last_Name"
mydatacol.HeaderText = "Last Name"
mydatacol.Width = 85 ' <-----------------------
ts1.GridColumnStyles.Add(mydatacol)

mydatacol = New DataGridTextBoxColumn
mydatacol.MappingName = "First_Name"
mydatacol.HeaderText = "First Name"
mydatacol.Width = 65 ' <-----------------------
ts1.GridColumnStyles.Add(mydatacol)

.............................

DataGrid1.TableStyles.Add(ts1)
Is that what you are looking for?

Nov 21 '05 #5

Not Aaron wrote:
The way I size my columns is through adding a bunch of
DataGridTextBoxColumn's to a DataGridTableStyle. I query about 10
items from my database. So I create 10 DataGridTextBoxColumn's and add them all the the tablestyle, then set the datagrid's tablestyle to the one i created. See:


I loaded my Datagrid with 6 or 7 columns then I ran your code, it had
no effect on my Datagrid. Is there something else I need to do? How
does the Datagrid know its supposed to use the DataGridTableStyle that
I added? What if there were 5 DataGridTableStyle objects in the
DataGridTableStyle collection? How would the Datagrid know which one to
use?

Chuck.

Nov 21 '05 #6

Not Aaron wrote:
Is that what you are looking for?


I got it to work. I used this microsoft tutorial which is similar to
what you were doing.

http://support.microsoft.com/default...b;en-us;811203

Thanks!

Nov 21 '05 #7

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

Similar topics

1
25121
by: Lance | last post by:
Hi, I would like to set column widths in my datagrid at run time. I tried DataGrid1.Columns(i).Width = xxx but it had no effect. I also looked at the design time properties page but was only...
3
5358
by: bismarkjoe | last post by:
Hello, I am trying to set the widths on the columns of a DataGrid component, and I'm not sure if I'm doing it correctly. My code is below: //load some inital data table = db.GetDataTable(...
3
3805
by: sck10 | last post by:
Hello, I am using a datagrid and am trying to control the column widths for each column. Is this possible? I have tried using the following: ControlStyle-BorderWidth="1000" with no apparent...
3
6451
by: Mike Fellows | last post by:
Im trying to set the column width of individual datgrid columns I can set the column widths of all the columns at once using Me.DataGrid1.PreferredColumnWidth = 100 But I am unable to set the...
1
2270
by: kpg | last post by:
Hi All, me again. I've got my web form data grid set up pretty nice. But how do you set the column width? The columns seem to grow and shrink based on the width of the data in them (that's ok)...
4
4183
by: Rich | last post by:
Hello, I have a one datagrid that will be based on different datatables. One datatable may have 7 columns, another 15... With the tables that have more columns, I have been manually dragging...
4
15274
by: Chuck | last post by:
I'm setting the column with for a gridview (25+- columns) and have paging turned on. When the gridview is first displayed, the column widths are all set to the default. But after paging to...
2
11700
by: Jacksm | last post by:
How can I align an asp:table columns with gridview columns (the widths)? I have tried setting table.column(0).width = gridview.column(0).width at page_load but it doesn't work. Thanks in advance
5
1936
by: vpravin | last post by:
Hi guys...i am a super noob with html and aspx.. i didnt start this project but i am modifying it... q: how do i change the width of a column: Thats the table which is within a text box ...
0
7048
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
7091
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...
1
6743
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
6966
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5344
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,...
1
4787
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...
0
2999
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1303
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 ...
1
564
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.