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