★搜Asp.net★(www.soAsp.net),为专业技术文档网站。
包括Asp.net开发技术文档·C#开发技术文档·Access/SQL Server数据库开发技术文档·VB.NET开发技术文档。
还包括·项目实战经验总结·开发经验技巧总结·项目开发心得。
GridView实现用“...”代替超长字符串
GridView实现用“...”代替超长字符串:
效果图:
解决方法:数据绑定后过滤每一行即可
 1for (int i = 0; i <= GridView1.Rows.Count - 1; i++)
 2{
 3    DataRowView mydrv;
 4    string gIntro;
 5    if (GridView1.PageIndex == 0)
 6    {
 7        mydrv = myds.Tables["飞狐工作室"].DefaultView[i];//表名
 8        gIntro = Convert.ToString(mydrv["家庭住址"]);//所要处理的字段
 9        GridView1.Rows[i].Cells[3].Text = SubStr(gIntro, 2);
10    }

11    else
12    {
13        mydrv = myds.Tables["飞狐工作室"].DefaultView[i + (5 * GridView1.PageIndex)];
14        gIntro = Convert.ToString(mydrv["家庭住址"]);
15        GridView1.Rows[i].Cells[3].Text = SubStr(gIntro, 2);
16    }

17}
 
调用的方法:
 1public string SubStr(string sString, int nLeng)
 2{
 3    if (sString.Length <= nLeng)
 4    {
 5        return sString;
 6    }

 7    string sNewStr = sString.Substring(0, nLeng);
 8    sNewStr = sNewStr + "";
 9    return sNewStr;
10}
 
后台全部代码:
  1using System;
  2using System.Data;
  3using System.Configuration;
  4using System.Web;
  5using System.Web.Security;
  6using System.Web.UI;
  7using System.Web.UI.WebControls;
  8using System.Web.UI.WebControls.WebParts;
  9using System.Web.UI.HtmlControls;
 10using System.Data.SqlClient;
 11public partial class _Default : System.Web.UI.Page
 12{
 13    SqlConnection sqlcon;
 14    SqlCommand sqlcom;
 15    string strCon = "Data Source=(local);Database=北风贸易;Uid=sa;Pwd=sa";
 16    protected void Page_Load(object sender, EventArgs e)
 17    {
 18        if (!IsPostBack)
 19        {
 20            ViewState["SortOrder"] = "身份证号码";
 21            ViewState["OrderDire"] = "ASC";
 22            bind();
 23        }

 24    }

 25    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
 26    {
 27        GridView1.EditIndex = e.NewEditIndex;
 28        bind();
 29    }

 30    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
 31    {
 32        string sqlstr = "delete from 飞狐工作室 where 身份证号码='" + GridView1.DataKeys[e.RowIndex].Value.ToString() + "'";
 33        sqlcon = new SqlConnection(strCon);
 34        sqlcom = new SqlCommand(sqlstr,sqlcon);
 35        sqlcon.Open();
 36        sqlcom.ExecuteNonQuery();
 37        sqlcon.Close();
 38        bind();
 39    }

 40    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
 41    {
 42        sqlcon = new SqlConnection(strCon);
 43        string sqlstr = "update 飞狐工作室 set 姓名='"
 44            + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text.ToString().Trim() + "',家庭住址='"
 45            + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[3].Controls[0])).Text.ToString().Trim() + "' where 身份证号码='" 
 46            + GridView1.DataKeys[e.RowIndex].Value.ToString() + "'";
 47        sqlcom=new SqlCommand(sqlstr,sqlcon);
 48        sqlcon.Open();
 49        sqlcom.ExecuteNonQuery();
 50        sqlcon.Close();
 51        GridView1.EditIndex = -1;
 52        bind();
 53    }

 54    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
 55    {
 56        GridView1.EditIndex = -1;
 57        bind();
 58    }

 59    public void bind()
 60    {
 61        string sqlstr = "select top 5 * from 飞狐工作室";
 62        sqlcon = new SqlConnection(strCon);
 63        SqlDataAdapter myda = new SqlDataAdapter(sqlstr, sqlcon);
 64        DataSet myds = new DataSet();
 65        sqlcon.Open();
 66        myda.Fill(myds, "飞狐工作室");
 67        GridView1.DataSource = myds;
 68        GridView1.DataKeyNames = new string[] { "身份证号码" };
 69        GridView1.DataBind();
 70        for (int i = 0; i <= GridView1.Rows.Count - 1; i++)
 71        {
 72            DataRowView mydrv;
 73            string gIntro;
 74            if (GridView1.PageIndex == 0)
 75            {
 76                mydrv = myds.Tables["飞狐工作室"].DefaultView[i];
 77                gIntro = Convert.ToString(mydrv["家庭住址"]);
 78                GridView1.Rows[i].Cells[3].Text = SubStr(gIntro, 2);
 79            }

 80            else
 81            {
 82                mydrv = myds.Tables["飞狐工作室"].DefaultView[i + (5 * GridView1.PageIndex)];
 83                gIntro = Convert.ToString(mydrv["家庭住址"]);
 84                GridView1.Rows[i].Cells[3].Text = SubStr(gIntro, 2);
 85            }

 86        }

 87        
 88        sqlcon.Close();
 89    }

 90    public string SubStr(string sString, int nLeng)
 91    {
 92        if (sString.Length <= nLeng)
 93        {
 94            return sString;
 95        }

 96        string sNewStr = sString.Substring(0, nLeng);
 97        sNewStr = sNewStr + "";
 98        return sNewStr;
 99    }

100    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
101    {
102        //如果是绑定数据行
103        if (e.Row.RowType == DataControlRowType.DataRow)
104        {
105            ////鼠标经过时,行背景色变
106            //e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='#E6F5FA'");
107            ////鼠标移出时,行背景色变
108            //e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='#FFFFFF'");
109            ////当有编辑列时,避免出错,要加的RowState判断
110            //if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate)
111            //{
112            //    ((LinkButton)e.Row.Cells[6].Controls[0]).Attributes.Add("onclick", "javascript:return confirm('你确认要删除:"" + e.Row.Cells[1].Text + ""吗?')");
113            //}
114        }

115        if (e.Row.RowIndex != -1)
116        {
117            int id = e.Row.RowIndex + 1;
118            e.Row.Cells[0].Text = id.ToString();
119        }

120    }

121}