How to wrap text in GridView Template columns in Asp.net.

Introduction:

Usually Gridview columns don’t wrap when the text content in the Gridview columns overflows the allocated width, it will keep expanding which makes the Gridview looks a bit odd.  To resolve this issue we need a custom implementation which will assign word wrap dynamically to GridView columns.

Problem Demo is given below

In my previous article I have explained the way to apply column wrap in GridView BoundField column. In this article I am going to explain how to apply word wrap for GridView’s Template columns.

Implementation:

You need to subscribe to GridView RowDatabound event and then apply the css styles to your respective columns in GridView.

         /// <summary>
        /// Handles the RowDataBound event of the GridView6 control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="GridViewRowEventArgs"/> instance containing the event data.</param>
        protected void GridView6_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                //here change the column index as per your GridVIew Design
                e.Row.Cells[0].Attributes.Add("style", "word-break:break-all;word-wrap:break-word;");
            }
        }

Complete Code

HTML

        <asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
        <asp:GridView ID="GridView6" runat="server" AutoGenerateColumns="False" DataKeyNames="FirstName" ForeColor="#333333" GridLines="Both" Width="250" OnRowDataBound="GridView6_RowDataBound">
            <AlternatingRowStyle BackColor="White" />
            <Columns>
                <asp:TemplateField HeaderText="Description" ItemStyle-Width="100px" HeaderStyle-Width="250px" >
                    <ItemTemplate>
                        <asp:Label ID="lblDescription" runat="server" Text='<%#Eval("FirstName") %>' Width="250" ></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>

C#:

 protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //Assign the data to GridView
                GridView6.DataSource = this.Get_LongText();
                //Bind the Grid
                GridView6.DataBind();
            }
        }

        //Populate some dummy data
        public DataTable Get_LongText()
        {

            DataTable dt = new DataTable();
            DataRow dr;
            string Item = "ThisisalongtextThisisalongtextThisisalongtextThisisalongtextThisisalongtextThisisalongtextThisisalongtextThisisalongtext,ThisisalongtextThisisalongtextThisisalongtextThisisalongtextThisisalongtextThisisalongtextThisisalongtextThisisalongtextThisisalongtextThisisalongtextThisisalongtext,Fruit2,Fruit3";
            string[] list = Item.Split(',');
            string Item2 = "ThisisalongtextThisisalongtextThisisalongtextThisisalongtextThisisalongtextThisisalongtextThisisalongtextThisisalongtext,ThisisalongtextThisisalongtextThisisalongtextThisisalongtextThisisalongtextThisisalongtextThisisalongtextThisisalongtextThisisalongtextThisisalongtextThisisalongtext,Fruit2,Fruit3";
            string[] list2 = Item2.Split(',');
            dt.Columns.Add("FirstName", typeof(string));
            dt.Columns.Add("LastName", typeof(string));
            for (int i = 0; i < list.Length; i++)
            {
                dr = dt.NewRow();
                dr["FirstName"] = list[i];
                dr["LastName"] = list2[i];
                dt.Rows.Add(dr);
            }
            return dt;
        }

protected void GridView6_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                //here change the column index as per your GridVIew Design
                e.Row.Cells[0].Attributes.Add("style", "word-break:break-all;word-wrap:break-word;");
            }
        }

Demo

Advertisements

, , , ,

  1. Leave a comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: