Saturday, 9 August 2014

How to Wrap the data of a Particular Column in GridView or Word Wrap for GridView columns in asp.net



wrap coloumn in Gridview

Description:-


in this example we explain that how to wrap the Gridview coloumn data in asp.net or wrap the data of the Gridview coloumn when data is long.

Suppose we have one form in which user information are entered by user like name,city,mono,address etc.... and finally bind this record to Gridview.

So here address enter by user is very long so by Default Gridview can Display data in a single line and Layout of the Gridview will not display proper. So that's type of situation we have to manually set the code for Gridview to particular coloumn so data will be display in wrap format in Gridview.

So apply this type of facility in your code just write the below code


Sorting Row data in gridview Gridview Sorting 

How to handle Concurrency in Linq to Sql Concurrency Example 



Default2.aspx:-

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>How to Wrap the data of a Particular Column in GridView || Word Wrap for GridView
        columns</title>
    <style type="text/css">
        .WordWrap
        {
            word-break: break-all;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <asp:GridView ID="grd_employee" runat="server" DataKeyNames="Emp_ID" AutoGenerateColumns="False"
        BackColor="White" BorderColor="#CC9966" BorderStyle="Solid" BorderWidth="1px"
        CssClass="WordWrap" CellPadding="4" Font-Names="Georgia" Font-Size="Small" Width="650px"
        AllowPaging="true">
        <Columns>
            <asp:TemplateField HeaderText="Emp_ID">
                <ItemTemplate>
                    <asp:Label ID="Label1" runat="server" Text='<%# Eval("Emp_ID")%>'></asp:Label>
                </ItemTemplate>
                <ItemStyle Width="100px" />
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Emp_Name">
                <ItemTemplate>
                    <asp:Label ID="Label2" runat="server" Text='<%# Eval("Emp_name")%>'></asp:Label>
                </ItemTemplate>
                <ItemStyle Width="100px" />
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Address">
                <ItemTemplate>
                    <asp:Label ID="Label3" runat="server" Text='<%# Eval("Address")%>'></asp:Label>
                </ItemTemplate>
                <ItemStyle Width="100px" />
            </asp:TemplateField>
        </Columns>
        <AlternatingRowStyle BackColor="#E6E6E1" />
        <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="#FFFFCC" />
    </asp:GridView>
    </form>
</body>
</html>


Default2.aspx.cs:-

using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System;
using System.Web.UI.WebControls;

public partial class Default2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        DataTable dt = new DataTable();
        dt.Columns.Add("Emp_ID");
        dt.Columns.Add("Emp_name");
        dt.Columns.Add("Address");

        DataRow dr = dt.NewRow();
        dr[0] = 1;
        dr[1] = "kirit";
        dr[2] = "gggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggg";
        dt.Rows.Add(dr);
        DataRow dr1 = dt.NewRow();
        dr1[0] = 2;
        dr1[1] = "pintu";
        dr1[2] = "yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy";
        dt.Rows.Add(dr1);
        grd_employee.DataSource = dt;
        grd_employee.DataBind();
    }
    protected void grd_employee_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Cells[2].Attributes.Add("Style", "word-break:break-all");

        }
    }
}

0 comments:

Post a Comment