GridView Column Width

J

Jen

I would like to set the width on 2 columns of a asp.net gridview control, but
I'm having trouble making this work. Here is the code I use to create the
gridview:

'Create a new data table
m_dsTable = New Data.DataTable
m_dsTable.Columns.Add("Name", System.Type.GetType("System.String"), "")
m_dsTable.Columns.Add("URL", System.Type.GetType("System.String"), "")

'Add two new command fields to select and delete
Dim pFld As New CommandField
pFld.ShowSelectButton = True
pFld.ButtonType = ButtonType.Image
GridView1.Columns.Add(pFld)

Dim pFld1 As New CommandField
pFld1.ShowDeleteButton = True
pFld1.ButtonType = ButtonType.Image
GridView1.Columns.Add(pFld1)

'Set the gridview datasource and bind the data
GridView1.DataSource = m_dsTable
GridView1.DataBind()

'Set the width of the command fields
GridView1.Columns(0).ItemStyle.Width = 5
GridView1.Columns(1).ItemStyle.Width = 5

After binding the table to the gridview, I have two fields that were created
in the datatable, Name and URL which I need to set the width on. I have tried
all number of ways to do so and nothing works. For some reason, I cannot
reference these columns with gridview1.columns(0).itemstyle.width. When I do
a count on the number of columns there are only 2 (only the command fields
are identified). Also, I have tried using the RowCreated event to cycle
through the cells and set the widths explicitly as follows:

Protected Sub GridView1_RowCreated(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowCreated

'Purpose: Set the column widths
If e.Row.RowType = DataControlRowType.DataRow Then
Dim td As TableCell
For Each td In e.Row.Cells
td.Width = 10
td.Wrap = True
Next
End If

End Sub

....and nothing happens in the gridview.
Any suggestions? I'm out of ideas.
Thanks!
 
B

Bob Barrows [MVP]

Jen said:
I would like to set the width on 2 columns of a asp.net gridview

There was no way for you to know it (except maybe by browsing through some
of the previous questions in this newsgroup before posting yours - always a
recommended practice) , but this is a classic asp newsgroup. ASP.Net bears
very little resemblance to classic ASP so, while you may be lucky enough to
find a dotnet-knowledgeable person here who can answer your question, you
can eliminate the luck factor by posting your question to a group where
those dotnet-knowledgeable people hang out. I suggest
microsoft.public.dotnet.framework.aspnet.
 
I

IGotyourdotnet

set it in the properties of the grid view or you can also set it in the
RowDatabound() method as well.

e.item.cell[2].width = 100;
e.item.cell[3].width = 100;

something like that.
 
N

no mat

can you fix this line
e.item.cell[2].width = 100;
like this
e.item.cell[2].width.Value = 100;
 

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

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top