11.GridView实现用“...”代替超长字符串:
效果图:
调用的方法:
1
public 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
}
后台全部代码:
1
using System;
2
using System.Data;
3
using System.Configuration;
4
using System.Web;
5
using System.Web.Security;
6
using System.Web.UI;
7
using System.Web.UI.WebControls;
8
using System.Web.UI.WebControls.WebParts;
9
using System.Web.UI.HtmlControls;
10
using System.Data.SqlClient;
11
public 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
}