Index

Player
Sample Image
Image
  • For Rent / Lease
  • For Sale
  • For Commerical
Test Conent Product ID : Kayak;
    how to display 20 characters asp net gridview columns using asp net
    http://www.aspdotnet-suresh.com/2010/12/how-to-show-limited-characters-in.html
    ------------------- Styl ----
.money { color:Red; }
.grid {width:970px; font-family:Tahoma;border:solid 5px #F2F2F2;margin:0px auto; }
.grid H2 { 	margin:0; padding:0; 	color:#ffffff;	height:20px;	line-height:30px;	font-size:14px;}
.grid .mid{	padding:3px 0px 0px 0px;}

.grid .datatable{	width:100%;	color:#666;border:none;}        

.grid .datatable TH
{
    font-size:12px;
    font-weight:bold;
    letter-spacing:0px;
    text-align:left;
    padding:2px 4px;
    color:#333333;
    border:none;
    border-bottom:solid 2px #bbd9ee;
    height:20px;
}
.grid .datatable TH A
{ 
	text-decoration:none;
	padding-right:18px; 
	color:#0066cc;
}
.grid .datatable TH.ASC A { background:url(F:/123kids/App_Themes/asc.gif) right center no-repeat; }

.grid .datatable TH.DESC A { background:url(F:/123kids/App_Themes/desc.gif) right center no-repeat; }


.grid .datatable .row TD
{
    font-size:11px;
    text-align:left;
    padding:6px 4px;
    border:none;
    border-bottom:solid 1px #bbd9ee;
    
}      
.grid .datatable .row:hover{background-color:#fffacd;color:#000;}

.grid .datatable .row TD.first { padding-left:10px;text-decoration:none; }

.grid .datatable .row TD.first A { text-decoration:none; }

.grid .datatable TH.first { padding-left:10px; }

.grid .datatable .row:hover .first
{
    background-repeat:no-repeat;
	background-image:url(../App_Themes/bullet.gif);background-position:left center ;
}  




/* Data Pager Styles */
.grid TR.pager-row TD
{
    border:none;
    border:solid 2px #bbd9ee;
}
.grid .pager
{
    font-family:arial,sans-serif;
    text-align:center;
    padding:6px;	
    border:0px solid white;
    font-size:14px;
} 
.grid .pager SPAN.command,    
.grid .pager SPAN.current,        
.grid .pager A.command,
.grid TR.pager-row TD A
{
    color:#5a90ce;
    padding:0px 4px;
    text-decoration:none;
    border:none;
    border:0px solid white;
}
.grid .pager A.command:hover,
.grid TR.pager-row TD A:hover
{
    
    background-color:#59A5D1;
    color:#fff;
    padding:0px 3px;
    text-decoration:none;
    border:none;
}        
.grid .pager SPAN.current,
.grid TR.pager-row TD SPAN
{
	border:none;
    font-weight:bold;
    color:#3e3e3e;
    padding:0px 6px;
}
.grid TR.pager-row TD
{ 
    border-bottom:none;
	border-top:none;
	text-align:center;
}
.grid TR.pager-row TABLE
{
	height:30px;
	margin:0 auto 0 auto;
	border:none;
}

-- style -----------


-------------- Aspx content  -----------------

asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">

link href="../../App_Themes/StyleSheet.css" rel="Stylesheet" type="text/css" />

  
/asp:Content>
asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">

div class="grid" style="height:auto;">
         asp:GridView ID="gvDATA" runat="server" AllowPaging="True" AllowSorting="True" RowStyle-BorderStyle="None"
               AutoGenerateColumns="false"  OnDataBound="gvDATA_DataBound" DataKeyNames="articleid"  onrowdatabound="gvDATA_RowDataBound"
               PageSize="20" CssClass="datatable" EmptyDataText="! No Rows Found"
               OnPageIndexChanging="gvDATA_PageIndexChanging"  OnSorting="gvDATA_Sorting" >
               HeaderStyle  CssClass="first" />
                Columns>
                   asp:HyperLinkField DataTextField="articleid" HeaderText="Articleid"   
                                       DataNavigateUrlFields="articleid"  SortExpression="articleid"
                                       DataNavigateUrlFormatString="../content/articles.aspx?M=2&ID={0}">  
                            HeaderStyle CssClass="first" />
                            ItemStyle CssClass="first"  />
                      /asp:HyperLinkField>

                      
                        asp:TemplateField HeaderText="Title">
                         ItemTemplate>
                              asp:Label ID="Label1" runat="server" Text='<%# Eval("Title") %>' tooltip='<%# Eval("Title") %>' >
                        /ItemTemplate>
                        /asp:TemplateField>
                        asp:BoundField HeaderText="author" DataField="author" SortExpression="author" />
                        asp:BoundField HeaderText="Date" DataField="date" SortExpression="date" />
                        asp:BoundField HeaderText="category" DataField="category" SortExpression="category" />
                    /Columns>

               PagerStyle CssClass="pager" />
               PagerTemplate>
                
                div class="pager">
                   asp:Button ID="btnFirst" runat="server" Text="<<" CommandArgument="First" CommandName="Page" />
                   asp:Button ID="btnPrev" runat="server" Text="<" CommandArgument="Prev" CommandName="Page" />
                   Page
                   asp:DropDownList ID="ddlPages" runat="server" AutoPostBack="True" OnSelectedIndexChanged="ddlPages_SelectedIndexChanged">  
                    nbsp;
                   asp:TextBox ID="txtPages" runat="server" Text="1" Width="30" OnTextChanged="txtPages_TextChanged" AutoPostBack="true" Visible="false"  >
                   of
                   asp:Label ID="lblPageCount" runat="server">
                   asp:Button ID="btnNext" runat="server" Text=">" CommandArgument="Next" CommandName="Page" />
                   asp:Button ID="btnLast" runat="server" Text=">>" CommandArgument="Last" CommandName="Page" />
                   /div>
               /PagerTemplate>
               RowStyle CssClass="row" />
               
           /asp:GridView>
    /div>

/asp:Content>


-- aspx.cs -----

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data.Common;
using System.Data;
using System.IO;
using System.Collections;
using System.Text;
using System.Text.RegularExpressions;
using System.Data.SqlClient;



static string cnSTR = "Data Source=204.93.178.157;Initial Catalog=leadgrab_db123kids;User ID=leadgrab_db123kidsadmin;PWD=dbadmin2";
    SqlConnection cn = new SqlConnection(cnSTR);
    DataSet dsDATA = new DataSet();
    SqlCommand cmdDATA = new SqlCommand();
    SqlDataAdapter daDATA;

    protected string videopath = "";

    public string ipath = "";
    public string vpath = "";

    string _pcount = "";
    private string seo_title = "";
    private string seo_description = "";
    private string seo_keywords = "";
    private string seo_logo_alternative = "";

    Hashtable PageURL = new Hashtable();
    int newID = 0;
    string strSQL = "";

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            //txtSearchTitle.Attributes.Add("onfocus", "this.value=''");

      
            fnFillData();

        }

    }
    private void fnFillData()
    {

        string strSQL = "";

        strSQL = "select articleid,title,category,author,convert(varchar(50),doe,106) as Date  from tbl_articles where status=1";


        strSQL = strSQL + " order by articleid desc";

        daDATA = new SqlDataAdapter(strSQL, cn);
        dsDATA.Clear();
        daDATA.Fill(dsDATA, "articles");

        gvDATA.DataSource = dsDATA.Tables["articles"].DefaultView;
        gvDATA.DataBind();


    }
    protected void gvDATA_DataBound(Object sender, EventArgs e)
    {
        GridViewRow gvrPager = gvDATA.BottomPagerRow;

        if (gvrPager == null) return;

        // get your controls from the gridview
        DropDownList ddlPages = (DropDownList)gvrPager.Cells[0].FindControl("ddlPages");
        Label lblPageCount = (Label)gvrPager.Cells[0].FindControl("lblPageCount");
        TextBox _txtPages = (TextBox)gvrPager.Cells[0].FindControl("txtPages");


        if (ddlPages != null)
        {
            // populate pager
            for (int i = 0; i < gvDATA.PageCount; i++)
            {

                int intPageNumber = i + 1;
                ListItem lstItem =
                new ListItem(intPageNumber.ToString());

                if (i == gvDATA.PageIndex)
                    lstItem.Selected = true;

                ddlPages.Items.Add(lstItem);
            }
        }

        int itemCount = 0;

        // populate page count
        if (lblPageCount != null)
        {

            //pull the datasource
            DataSet ds = gvDATA.DataSource as DataSet;
            DataView dv = gvDATA.DataSource as DataView;
            if (ds != null)
            {
                itemCount = ds.Tables[0].Rows.Count;
            }
            else
            {
                itemCount = dv.Count;
            }

            string pageCount = "" + gvDATA.PageCount.ToString() + " (" + itemCount.ToString() + " Articles )";
            lblPageCount.Text = pageCount;

            _txtPages.Text = _pcount;
        }

        Button btnPrev = (Button)gvrPager.Cells[0].FindControl("btnPrev");
        Button btnNext = (Button)gvrPager.Cells[0].FindControl("btnNext");
        Button btnFirst = (Button)gvrPager.Cells[0].FindControl("btnFirst");
        Button btnLast = (Button)gvrPager.Cells[0].FindControl("btnLast");

        //now figure out what page we're on
        if (gvDATA.PageIndex == 0)
        {
            btnPrev.Enabled = false;
            btnFirst.Enabled = false;
        }
        else if (gvDATA.PageIndex + 1 == gvDATA.PageCount)
        {
            btnLast.Enabled = false;
            btnNext.Enabled = false;
        }
        else
        {
            btnLast.Enabled = true;
            btnNext.Enabled = true;
            btnPrev.Enabled = true;
            btnFirst.Enabled = true;
        }

    }
    protected void gvDATA_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        int i = 0;
        e.Row.Cells[1].Visible = false;
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            foreach (TableCell cell in e.Row.Cells)
            {
                i++;
                string s = cell.Text;
                if (cell.Text.Length > 20 && (i == 3))
                    cell.Text = cell.Text.Substring(0, 20) + "....";
                cell.ToolTip = s;
            }
        }
    }
    protected void ddlPages_SelectedIndexChanged(Object sender, EventArgs e)
    {
        GridViewRow gvrPager = gvDATA.BottomPagerRow;
        DropDownList ddlPages = (DropDownList)gvrPager.Cells[0].FindControl("ddlPages");

        gvDATA.PageIndex = ddlPages.SelectedIndex;
        fnFillData();
    }
    protected void gvDATA_Sorting(object sender, GridViewSortEventArgs e)
    {

        strSQL = "select articleid,title,category,author,convert(varchar(50),doe,106) as Date from tbl_articles";

        daDATA = new SqlDataAdapter(strSQL, cn);

        dsDATA.Clear();
        daDATA.Fill(dsDATA, "review_details");



        if (dsDATA != null)
        {
            DataView dataView = new DataView(dsDATA.Tables[0]);
            dataView.Sort = e.SortExpression + " " + GetSortDirection(e.SortExpression);

            gvDATA.DataSource = dataView;
            gvDATA.DataBind();
        }

    }
    string GetSortDirection(string sortBy)
    {

        string sortDir = " ASC";
        if (ViewState["sortBy"] != null)
        {
            string sortedBy = ViewState["sortBy"].ToString();

            if (sortedBy == sortBy)
            {
                //the direction should be desc
                sortDir = " DESC";

                //reset the sorter to null
                ViewState["sortBy"] = null;
            }
            else
            {
                //this is the first sort for this row
                //put it to the ViewState
                ViewState["sortBy"] = sortBy;

            }
        }
        else
        {
            //it's null, so this is the first sort
            ViewState["sortBy"] = sortBy;
        }
        return sortDir;

    }
    protected void gvDATA_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gvDATA.PageIndex = e.NewPageIndex;
        //rebind using your bind routine
        fnFillData();
    }
    protected void txtPages_TextChanged(object sender, EventArgs e)
    {
        GridViewRow gvrPager = gvDATA.BottomPagerRow;

        //DropDownList ddlPages = (DropDownList)gvrPager.Cells[0].FindControl("ddlPages");

        TextBox txtpages = (TextBox)gvrPager.Cells[0].FindControl("txtPages");
        _pcount = txtpages.Text.ToString();
        gvDATA.PageIndex = int.Parse(txtpages.Text.ToString());
        //gvDATA.PageIndex = ddlPages.SelectedIndex;

        // a method to populate your grid
        fnFillData();
    }