Setting column widths in a datagrid.

Setting column widths in a datagrid.

Post by cr11 » Sun, 16 Jan 2005 02:07:27



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!!!!
 
 
 

Setting column widths in a datagrid.

Post by Herfried K » Sun, 16 Jan 2005 02:25:19

< XXXX@XXXXX.COM > schrieb:

<URL: http://www.yqcomputer.com/ ;
-> "Setting Column Width"

--
M S Herfried K. Wagner
M V P <URL: http://www.yqcomputer.com/ ;
V B <URL: http://www.yqcomputer.com/ ;

 
 
 

Setting column widths in a datagrid.

Post by Elliot Rod » Sun, 16 Jan 2005 02:29:07

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
 
 
 

Setting column widths in a datagrid.

Post by Elliot Rod » Sun, 16 Jan 2005 02:30:46

whoops - I took it for granted that you were putting this together in
ASP.NET. Never mind.
 
 
 

Setting column widths in a datagrid.

Post by Not Aaro » Sun, 16 Jan 2005 03:32:09

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?
 
 
 

Setting column widths in a datagrid.

Post by cr11 » Sun, 16 Jan 2005 06:08:09


add
the

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.
 
 
 

Setting column widths in a datagrid.

Post by cr11 » Sun, 16 Jan 2005 08:32:41


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

http://www.yqcomputer.com/ ;en-us;811203

Thanks!