469,140 Members | 1,355 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,140 developers. It's quick & easy.

GridView Column Width

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!

Jun 6 '07 #1
3 17243
Jen wrote:
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.

--
Microsoft MVP - ASP/ASP.NET
Please reply to the newsgroup. This email account is my spam trap so I
don't check it very often. If you must reply off-line, then remove the
"NO SPAM"
Jun 6 '07 #2
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.


"Jen" <Je*@discussions.microsoft.comwrote in message
news:68**********************************@microsof t.com...
>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!

Jun 8 '07 #3
can you fix this line
e.item.cell[2].width = 100;
like this
e.item.cell[2].width.Value = 100;



*** Sent via Developersdex http://www.developersdex.com ***
Jun 18 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by msch-prv | last post: by
2 posts views Thread by Samuel Shulman | last post: by
6 posts views Thread by =?Utf-8?B?TWlrZSBQYXJyaXM=?= | last post: by
5 posts views Thread by mohaaron | last post: by
2 posts views Thread by =?Utf-8?B?cm9kY2hhcg==?= | last post: by
4 posts views Thread by Yin99 | last post: by
1 post views Thread by Durango2008 | last post: by
2 posts views Thread by =?Utf-8?B?ZG1idXNv?= | last post: by
1 post views Thread by =?Utf-8?B?ZG1idXNv?= | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.