日期:2014-05-18  浏览次数:20531 次

关于GridView实现用“...”代替超长字符串
月儿原创那学习了 GridView实现用“...”代替超长字符串 代码

使用在了一个可编辑的列中,但是使用“...”代替超长字符串后,不能编辑了,点击编辑不会出现文本框,删除“...”代替超长字符串的代码后才可以,大侠们帮忙看看哪里出现的问题

C# code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
//引用命名空间
using System.Data;
using System.Data.OleDb;

public partial class _Default : System.Web.UI.Page
{
    //数据库连接字符串

    public static string connstring = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + System.Web.HttpContext.Current.Server.MapPath("~/App_Data/CODR_Database.mdb");

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {
            bind();
        }
    }
    public void bind()
    {
        string sql;
        sql = "select * from CaseList order by id,FINANCE_WEEK desc";
        OleDbConnection conn = new OleDbConnection();//创建连接对象
        conn.ConnectionString = connstring;//给连接字符串赋值
        conn.Open();//打开数据库
        OleDbDataAdapter Dar = new OleDbDataAdapter(sql, connstring);

        DataSet ds = new DataSet();
        Dar.Fill(ds,"CaseList");
        GridView1.DataSource = ds;
        GridView1.DataBind();

        //GridView实现用“...”代替超长字符串
        for (int i = 0; i <= GridView1.Rows.Count - 1; i++)
        {
            DataRowView mydrv;
            string gIntro;

            if (GridView1.PageIndex == 0)
            {
                mydrv = ds.Tables["CaseList"].DefaultView[i];
                gIntro = Convert.ToString(mydrv["REMARK_Simple"]);
                GridView1.Rows[i].Cells[11].Text = SubStr(gIntro, 2);
                gIntro.ToString();
            }
            else
            {
                mydrv = ds.Tables["CaseList"].DefaultView[i + (5 * GridView1.PageIndex)];
                gIntro = Convert.ToString(mydrv["REMARK_Simple"]);
                GridView1.Rows[i].Cells[11].Text = SubStr(gIntro, 2);

            }
        }
        conn.Close();//关闭数据库

    }
    //GridView实现用“...”代替超长字符串
    public string SubStr(string sString, int nLeng)
    {
        if (sString.Length <= nLeng)
        {
            return sString;
        }
        string sNewStr = sString.Substring(0, nLeng);
        sNewStr = sNewStr + "...";
        return sNewStr;
    }
    //分页功能
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        bind();
    }
    //取消编辑状态
    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridView1.EditIndex = -1;
        bind();
    }
    //进入编辑状态
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
        bind();
    }
    //数据更新
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        int upid = (int)GridView1.DataKeys[e.RowIndex].Value;
        string p = ((TextBox)GridView1.Rows[e.RowIndex].Cells[10].Controls[0]).Text;
        string u = ((TextBox)GridView1.Rows[e.RowIndex].Cells[11].Controls[0]).Text;

        string str = "update CaseList set pod='" + p + "',REMARK_Simple='" + u + "' where id=" + upid;

        OleDbConnection conn = new OleDbConnection();//创建连接对象
        conn.ConnectionString = connstring;//给连接字符串赋值
        conn.Open();//打开数据库
        OleDbCommand cmd = new OleDbCommand(str, conn);
        cmd.ExecuteNonQuery();//
        conn.Close();//关闭数据库
        Response.Write("<script langeuage=javascript>;alert('更新成功!');</script>");
        GridView1.EditIndex = -1;
        bind();
    }

}



------解决方案--------------------