Notices

Great community. Great ideas.

Welcome to ASP/Free, a community dedicated to helping beginners and professionals alike in improving their knowledge of Microsoft's development and administration technologies. Sign up today to gain access to the combined insight of tens of thousands of members.

Thread: Assign Width of Column in Gridview

Results 1 to 10 of 10
Share This Thread →
  1. #1
    Carliviris's Avatar
    Carliviris is offline Maco User
    Join Date
    Nov 2007
    Location
    Santo Domingo
    Posts
    130
    Rep Power
    8

    Assign Width of Column in Gridview

    Hi

    I wanna Assign the Width of Column in Gridview?
    How i can do this?

    I tried to do:

    Code:
     e.Row.Cells(i).Width = 500
    But, not work.

    Help please

  2. #2
    viplort is offline Contributing User
    Join Date
    Jan 2007
    Posts
    315
    Rep Power
    73
    Quote Originally Posted by Carliviris
    Hi

    I wanna Assign the Width of Column in Gridview?
    How i can do this?

    I tried to do:

    Code:
     e.Row.Cells(i).Width = 500
    But, not work.

    Help please
    Try using
    Code:
    GridView1.Columns(i).HeaderStyle.Width = Unit.Pixel(whatever)
    Last edited by viplort; July 11th, 2008 at 04:42 PM.

  3. #3
    Carliviris's Avatar
    Carliviris is offline Maco User
    Join Date
    Nov 2007
    Location
    Santo Domingo
    Posts
    130
    Rep Power
    8
    i tried but this not work


    Quote Originally Posted by viplort
    Try using
    Code:
    GridView1.Columns(i).HeaderStyle.Width = Unit.Pixel(whatever)

  4. #4
    viplort is offline Contributing User
    Join Date
    Jan 2007
    Posts
    315
    Rep Power
    73
    Quote Originally Posted by Carliviris
    i tried but this not work
    Try this as well if the unit.pixel doesn't work, just use the number you want to size it to:
    Code:
    Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
    
    If e.Row.RowType = DataControlRowType.Header Then
    GridView.Columns(i).HeaderStyle.Width = "90"<--- or whatever number size
    End If
    End Sub
    If that doesn't work please post your code? You might have your gridview set at a particular width and changing a column width wont show the change (as a possibility). But I cant tell unless I see your code.
    Last edited by viplort; July 15th, 2008 at 02:44 PM.

  5. #5
    Carliviris's Avatar
    Carliviris is offline Maco User
    Join Date
    Nov 2007
    Location
    Santo Domingo
    Posts
    130
    Rep Power
    8
    What code specifically i can post here?
    i did the solution of viplort
    This code not work for me.

  6. #6
    viplort is offline Contributing User
    Join Date
    Jan 2007
    Posts
    315
    Rep Power
    73
    Quote Originally Posted by Carliviris
    What code specifically i can post here?
    i did the solution of viplort
    This code not work for me.
    Post your html of the Gridview and the codebehind sub where your setting the width value

  7. #7
    Carliviris's Avatar
    Carliviris is offline Maco User
    Join Date
    Nov 2007
    Location
    Santo Domingo
    Posts
    130
    Rep Power
    8
    The html code of gridview:


    Code:
    <asp:GridView ID="GridView1" runat="server" BackColor="#DEBA84" BorderColor="#DEBA84"
                BorderStyle="None" BorderWidth="1px" CellPadding="3" CellSpacing="2" Height="312px"
                Style="z-index: 100; left: 0px; position: absolute; top: 0px" Width="256px" AutoGenerateColumns="False" Font-Size="XX-Small">
                <FooterStyle BackColor="#F7DFB5" ForeColor="#8C4510" />
                <RowStyle BackColor="#FFF7E7" Font-Size="X-Small" ForeColor="#8C4510" />
                <PagerStyle ForeColor="#8C4510" HorizontalAlign="Center" />
                <SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="White" />
                <HeaderStyle BackColor="#A55129" Font-Bold="True" Font-Size="Small" ForeColor="White" />

    And the subcode:


    Code:
    Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
            If e.Row.RowType = DataControlRowType.Header Then
                GridView1.Columns(1).HeaderStyle.Width = 1500
            End If
        End Sub

  8. #8
    viplort is offline Contributing User
    Join Date
    Jan 2007
    Posts
    315
    Rep Power
    73
    Quote Originally Posted by Carliviris
    The html code of gridview:


    Code:
    <asp:GridView ID="GridView1" runat="server" BackColor="#DEBA84" BorderColor="#DEBA84"
                BorderStyle="None" BorderWidth="1px" CellPadding="3" CellSpacing="2" Height="312px"
                Style="z-index: 100; left: 0px; position: absolute; top: 0px" Width="256px" AutoGenerateColumns="False" Font-Size="XX-Small">
                <FooterStyle BackColor="#F7DFB5" ForeColor="#8C4510" />
                <RowStyle BackColor="#FFF7E7" Font-Size="X-Small" ForeColor="#8C4510" />
                <PagerStyle ForeColor="#8C4510" HorizontalAlign="Center" />
                <SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="White" />
                <HeaderStyle BackColor="#A55129" Font-Bold="True" Font-Size="Small" ForeColor="White" />

    And the subcode:


    Code:
    Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
            If e.Row.RowType = DataControlRowType.Header Then
                GridView1.Columns(1).HeaderStyle.Width = 1500
            End If
        End Sub
    At first glance here your setting the width of your Grid to 256px and then telling your header to be a width of 1500px (6x's larger then the allowable width of 256).
    If you want this particular column larger you need to make the gridview width larger or it wont do anything. So it needs to be at least 1500 as well for that column header to be wider.
    So declaring a gridview width to say 500px leaves you only 500px of room to be shared between all your columns.
    Last edited by viplort; July 16th, 2008 at 01:32 PM.

  9. #9
    Carliviris's Avatar
    Carliviris is offline Maco User
    Join Date
    Nov 2007
    Location
    Santo Domingo
    Posts
    130
    Rep Power
    8
    Thanks man, i'll try this

  10. #10
    viplort is offline Contributing User
    Join Date
    Jan 2007
    Posts
    315
    Rep Power
    73
    Quote Originally Posted by Carliviris
    Thanks man, i'll try this
    NP.....let me know if that works for you (it should though).

Share This Thread →

Become Part of This Conversation

Join NowFor Free!

Similar Threads

  1. Gridview onmouseover (only first header column)
    By viplort in forum .NET Development
    Replies: 2
    Last Post: February 2nd, 2007, 03:37 PM
  2. Gridview - get column index number
    By Zath in forum .NET Development
    Replies: 1
    Last Post: December 28th, 2006, 02:56 PM
  3. Width of Column
    By Beerman3436 in forum ASP Development
    Replies: 1
    Last Post: December 13th, 2006, 08:18 AM
  4. CheckBox in GridView to change row column
    By KevCB226 in forum .NET Development
    Replies: 3
    Last Post: June 5th, 2006, 09:07 AM
  5. Determine if field is autonumber
    By dwe in forum ASP Development
    Replies: 14
    Last Post: November 16th, 2004, 05:21 PM

ASP Free Advertisers and Affiliates