http://www.cnblogs.com/waynewjp/archive/2009/09/06/1561425.html
protected void gvTest_RowDataBound(object sender, GridViewRowEventArgs e)
{
//如果是綁定數據行
if (e.Row.RowType == DataControlRowType.DataRow)
{
//鼠標經過時,行背景色變
//e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='#E6F5FA'");
//鼠標移出時,行背景色變
//e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='#FFFFFF'");
//Row鼠標經過時,行背景色變(使用CSS方式)
e.Row.Attributes.Add("onmouseover", "this.className='over'");
//鼠標移出時,行背景色變
e.Row.Attributes.Add("onmouseout", "this.className='out'");
//Cells鼠標經過時,行背景色變(使用CSS方式)
//e.Row.Cells[1].Attributes.Add("onmouseover", "this.className='over'");
//鼠標移出時,行背景色變
//e.Row.Cells[1].Attributes.Add("onmouseout", "this.className='out'");
//為gridview行添加雙擊事件
string url = "http://www.udn.com/";
e.Row.Attributes.Add("ondblclick", "javascript:parent.location.href='"+url+"'");
//為gridview列添加click事件
e.Row.Cells[3].Attributes.Add("onclick", "javascript:parent.location.href='" + url + "'");
}
//如果是綁定數據行
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate)
{
//刪除前彈出確認框
((LinkButton)e.Row.Cells[6].Controls[0]).Attributes.Add("onclick", "javascript:return confirm('你確認要刪除:\"" + e.Row.Cells[2].Text + "\"嗎?')");
}
}
if (e.Row.RowIndex != -1)//自動生成編碼列
{
int id = e.Row.RowIndex + 1;
e.Row.Cells[0].Text = id.ToString();
}
if (e.Row.RowIndex != -1)//過長的內容,用...代替
{
string strInfo = e.Row.Cells[2].Text;
e.Row.Cells[2].Text = SubStr(strInfo, 1);
e.Row.Cells[2].ToolTip = strInfo;
}
if (e.Row.RowIndex == 5)//突出顯示某信息
{
e.Row.Cells[0].BackColor = System.Drawing.Color.Red;
}
}
public string SubStr(string sString, int nLeng)
{
if (sString.Length <= nLeng)
{
return sString;
}
string sNewStr = sString.Substring(0, nLeng);
sNewStr = sNewStr + "...";
return sNewStr;
}
http://blog.csdn.net/21aspnet/article/details/1540301
public void bind()
{
string sqlstr = "select top 10 * from 飛狐工作室";
sqlcon = new SqlConnection(strCon);
SqlDataAdapter myda = new SqlDataAdapter(sqlstr, sqlcon);
DataSet myds = new DataSet();
sqlcon.Open();
myda.Fill(myds, "飛狐工作室");
GridView1.DataSource = myds;
GridView1.DataKeyNames = new string[] { "身份證號碼" };
GridView1.DataBind();
for (int i = 0; i <= GridView1.Rows.Count - 1; i++)
{
DataRowView mydrv = myds.Tables["飛狐工作室"].DefaultView[i];
string score = Convert.ToString(mydrv["起薪"]);
if (Convert.ToDouble(score) < 34297.00)//大家這裡根據具體情況設置可能ToInt32等等
{
GridView1.Rows[i].Cells[4].BackColor = System.Drawing.Color.Red;
}
}
sqlcon.Close();
}
}
首先設置<asp:BoundField DataField="家庭住址" HeaderText="家庭住址" ItemStyle-Width="100" />
gridview裡有一列綁定的數據很長,顯示的時候在一行裡面顯示,頁面拉得很寬。
原因是連續英文段為一個整體導致的,在RowDataBound中添加上了一句e.Row.Cells[2].Style.Add("word-break", "break-all")就可以。
如果要給所有的列增加此屬性:
protected void Page_Load(object sender, EventArgs e)
{
//正常換行
GridView1.Attributes.Add("style", "word-break:keep-all;word-wrap:normal");
//下面這行是自動換行
GridView1.Attributes.Add("style", "word-break:break-all;word-wrap:break-word");
if (!IsPostBack)
{
bind();//調用數據綁定即可
}
}
總之:善用CSS的word-break:break-all;word-wrap:break-word屬性即可,這個屬性是通用的對於頑固的南換行問題都可以解決,不侷限於GridView。