473,406 Members | 2,352 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

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 17508
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: msch-prv | last post by:
I have difficulties adjusting the column width of a gridview control. The column width changes appear in the design grid, but at run-time the width changes back to what I presume are the grid's...
2
by: Samuel Shulman | last post by:
The GridView show an image that takes say 75px in height then I set the next column to 200px wide because I don't want the text to wrap However it still wraps the text and the width of the...
6
by: =?Utf-8?B?TWlrZSBQYXJyaXM=?= | last post by:
I am trying to set the width of one of the columns of an asp.net 2.0 Gridview control. I am using a line - MyGridview.Columns.ItemStyle.Width = Unit.Percentage(40); The problem is that there...
5
by: mohaaron | last post by:
OK, I have now gone through lots of posts, and there are lots, looking for a anwer as to why I can't set the width of the columns in my gridview. So far I have tried the following methods to set...
2
by: =?Utf-8?B?cm9kY2hhcg==?= | last post by:
hey all, can someone please tell me how to make a gridview column fixed-width? When I resize the browser the columns auto adjust somehow and i can't figure out how to make that stop for one of my...
4
by: Yin99 | last post by:
I have a Gridview binding to a DataTable source. I'd like to set the column with of the second column. I cannot do this apparently because when AutoGenerateColumns=true, they do not appear in the...
1
by: Durango2008 | last post by:
Hi, I am having a bit of trouble with my gridview table and the width of the columns. The gridview is made of Templates that contain LinkButton controls. The LinkButtons will display a string...
2
by: =?Utf-8?B?ZG1idXNv?= | last post by:
How do I increase the default width of a gridview column (just one column, the 10th column, named "Notes")? In design mode, I changed the width properties of the HeaderStyle, ControlStyle, and...
1
by: =?Utf-8?B?ZG1idXNv?= | last post by:
How do I increase the default width of a gridview column (just one column, the 10th column, named "Notes")? In design mode, I changed the width properties of the HeaderStyle, ControlStyle, and...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...
0
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
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...

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.