GridView Column Width

GridView Column Width

am 06.06.2007 03:16:00 von 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!

Re: GridView Column Width

am 06.06.2007 03:40:12 von reb01501

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"

Re: GridView Column Width

am 08.06.2007 15:21:20 von 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.






"Jen" wrote in message
news:688BF554-6B4C-4094-A6A0-71361CA24BC1@microsoft.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!
>
>
>

Re: GridView Column Width

am 18.06.2007 12:07:04 von no mat

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