Gridview Column widths

Last post 06-19-2007 5:21 AM by Allen Chen – MSFT. 7 replies.

Sort Posts:

  • Gridview Column widths

    06-12-2007, 10:35 PM
    • Member
      12 point Member
    • kalok
    • Member since 06-13-2007, 12:25 AM
    • Posts 6

    I'm displaying a gridview2 within a gridview1 row and want to set the column widths of gridview2 to be consistent across all gridview2s.

    i.e If row 1 automatically sizes gridview2 colums 1,2,3 to 10,20,30 and row 2 automatically sizes gridview2 columns to 5,25,20 , I want to resize each gridview2 in all rows to 10,25,30

    I've tried reading the item style on each gridview column and I get back what I've loaded not what is about to be rendered.

    I've used the following code to attempt this with not much success.

     

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)

    Dim Gv1 As GridView

    Dim Gv2 As GridView

    Dim Gv1r As GridViewRow

    Dim R1 As Integer

    Dim i As Integer

    Dim G2Columns() As Integer

    Gv1 = GridView1

    Gv2 = Gv1.Rows(0).FindControl(
    "GridView2")

    ReDim G2Columns(Gv2.Columns.Count)

    For R1 = 0 To Gv1.Rows.Count - 1

    Gv1r = Gv1.Rows(R1)

    Gv2 = Gv1r.FindControl(
    "GridView2")For i = 0 To Gv2.Columns.Count - 1

    If Gv2.Columns(i).ControlStyle.Width.Value > G2Columns(i) Then

    G2Columns(i) = Gv2.Columns(i).ControlStyle.Width.Value

    End If

    Next

    Next

    For R1 = 0 To Gv1.Rows.Count - 1

    Gv1r = Gv1.Rows(R1)

    Gv2 = Gv1r.FindControl(
    "GridView2")For i = 0 To Gv2.Columns.Count - 1

    Gv2.Columns(i).ItemStyle.Width = G2Columns(i)

    Next

    Next

    End Sub

     

    Is there any way of getting the actual rendered size of each column?

    Any help would be appreciated

     

  • Re: Gridview Column widths

    06-14-2007, 10:38 PM

    Hi:
      It's difficult to know the width of a column because the long string will expand the cell. And it's expected to do this by default.

     You can define width for each column and add this css:


    <td style="word-break:break-all">

      In this way you can get fixed width.

     Note some Explorers may not recognize this css.

    Regards

    Sincerely,
    Allen Chen
    Microsoft Online Community Support

    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
  • Re: Gridview Column widths

    06-19-2007, 3:13 AM
    • Member
      12 point Member
    • kalok
    • Member since 06-13-2007, 12:25 AM
    • Posts 6

    Hi Allen

    Unfortunately each row will show different details in the second gridview and using fixed sizes would impact the data being presented.

    It's more for aesthetic reasons I want to resize them all to be the same

    Regards 

  • Re: Gridview Column widths

    06-19-2007, 3:27 AM

    Hi:

      In GridView's PreRender, we can get the instance of each TableCell and change their style.

      You can set a breakpoint in this event handler and test:

    this.GridView1.Controls[0].Controls[Row_Index].Controls[TableCell_Index].........

      to see the hierarchy and you can adjust all column's width using a for loop. Besides, using the css I mentioned in my previous post.

    Hope it helps.

    Regards

    Sincerely,
    Allen Chen
    Microsoft Online Community Support

    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
  • Re: Gridview Column widths

    06-19-2007, 4:06 AM
    • Member
      12 point Member
    • kalok
    • Member since 06-13-2007, 12:25 AM
    • Posts 6

    Hi Allen

    I don't quite understand what you're saying.  I attempted this in my original logic (above) even in the PreRender

    Regards

  • Re: Gridview Column widths

    06-19-2007, 4:17 AM

    Hi:

      Did you also add this css?

    <td style="word-break:break-all">

     And remove the width of the GridView? If you have other css, just remove it and try again.

     

     What's the final effect do you get with your code?

      As I mentioned, long string will expand the TableCell (td). So if you don't like fixed width, an alternative method is to add extra code to assign width to these TableCell according to the length of the text in it. (dynamically fixed)

    Thanks

    Sincerely,
    Allen Chen
    Microsoft Online Community Support

    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
  • Re: Gridview Column widths

    06-19-2007, 4:37 AM
    • Member
      12 point Member
    • kalok
    • Member since 06-13-2007, 12:25 AM
    • Posts 6

    Hi Allen

    I just tried it an it made a mess of the screen. I'll have to resort to calculating the width of the text (dynamically fixed).

    I was able to do this in VB6 yet cannot in ASP.NET (VWD)

    Can you point me to any code snippets to do this?

    Regards

  • Re: Gridview Column widths

    06-19-2007, 5:21 AM
    Answer

    Hi:

      Actually GridView will render as <table> (Html markup). You can view source to see how the text in <td> will influence the width. So it's different with VB6.

      I think your problem can be solved by css. You can simply assign a well-designed css to the parent and nested GridView. Here's just a simple sample using one GridView. Assuming there're three columns:

    <head runat="server">
        <title>Untitled Page</title>
        <style type="text/css">
        .td1{width:5px; }
        .td2{width:20px;}
        .td3{width:40px; }
        td{word-break:break-all;}
        th{word-break:break-all;}
        </style>
    </head>

    ...

       <asp:GridView ID="GridView1" runat="server" DataSourceID="SqlDataSource1">
            </asp:GridView>

    ...

     Protected Sub GridView1_RowCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowCreated

    'Check length if needed
            e.Row.Cells(0).CssClass = "td1"
            e.Row.Cells(1).CssClass = "td2"
            e.Row.Cells(2).CssClass = "td3"

        End Sub

     

     

     You can also try percentage value:

       .td3{width:40%; }

    Hope it helps.

    Regards

    Sincerely,
    Allen Chen
    Microsoft Online Community Support

    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Page 1 of 1 (8 items)