Setting column widths in a datagrid.

C

cr113

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

Elliot Rodriguez

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
 
E

Elliot Rodriguez

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

Not Aaron

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?
 
C

cr113

Not said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top