Danish Mugal Danish Mugal - 1 month ago 13
Vb.net Question

How to get GridView Header Text vertically

I am using grid view to show data but i have many columns with big name.so it contains more screen size.

So please help me with how to get header text vertically so that column doesn't get more screen and i can show my whole grid in same page without scrolling.

I am using visual studio 2005 with vb.net

Any help will be appreciated.

Answer

This will work. Although I have to say it looks ugly AF if all the header texts are displayed vertically.

First we need a CSS class.

<style>
    .VerticalHeaderText {
        white-space: pre-wrap;
        word-wrap: break-word;
        width: 1px;
        //line-height needs some tweaking for font size, type etc
        line-height: 75%;
    }
</style>

Then we need to wrap the header texts in a container that has the class VerticalHeaderText. For this we use the GridViews OnRowDataBound event.

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.Header)
    {
        for (int i = 0; i < e.Row.Cells.Count; i++)
        {
            e.Row.Cells[i].Text = "<div class=\"VerticalHeaderText\">" + e.Row.Cells[i].Text + "</div>";
        }
    }
}

In VB

Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
    If (e.Row.RowType = DataControlRowType.Header) Then
        Dim i As Integer = 0
        Do While (i < e.Row.Cells.Count)
            e.Row.Cells(i).Text = ("<div class=""VerticalHeaderText"">"  _
                        + (e.Row.Cells(i).Text + "</div>"))
            i = (i + 1)
        Loop 
    End If
End Sub