GridView Column Width

Discussion in 'ASP General' started by Jen, Jun 6, 2007.

  1. Jen

    Jen Guest

    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!
     
    Jen, Jun 6, 2007
    #1
    1. Advertising

  2. 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"
     
    Bob Barrows [MVP], Jun 6, 2007
    #2
    1. Advertising

  3. 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:...
    >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!
    >
    >
    >
     
    IGotyourdotnet, Jun 8, 2007
    #3
  4. Jen

    no mat Guest

    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 ***
     
    no mat, Jun 18, 2007
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. AndrewF
    Replies:
    1
    Views:
    841
    Bruce Barker
    Oct 10, 2005
  2. Keith G Hicks
    Replies:
    3
    Views:
    806
    Keith G Hicks
    Feb 18, 2008
  3. Replies:
    0
    Views:
    974
  4. Carl Howarth

    DG Column Width vs Table Column Width

    Carl Howarth, Oct 22, 2004, in forum: ASP .Net Datagrid Control
    Replies:
    1
    Views:
    291
    Eliyahu Goldin
    Oct 24, 2004
  5. Replies:
    1
    Views:
    1,598
Loading...

Share This Page