1. 为按钮增加确认对话框。在RowDataBound事件中,为按钮增加"onclick"事件属性
protected
void
gvVersionList_RowDataBound(
object
sender, GridViewRowEventArgs e)
... {
//如果是绑定数据行
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[1].Text + ""吗?')");
}
}
}
... {
//如果是绑定数据行
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[1].Text + ""吗?')");
}
}
}
2.鼠标移到GridView某一行时改变该行的背景色。和1的做法基本一样,在在RowDataBound事件中,为按钮增加"onmouseover"事件和"onmouseout"事件属性
protected
void
GridView1_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'");
}
}
... {
//如果是绑定数据行
if (e.Row.RowType == DataControlRowType.DataRow)
...{
//鼠标经过时,行背景色变
e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='#E6F5FA'");
//鼠标移出时,行背景色变
e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='#FFFFFF'");
}
}
3.为每一行增加一个行号
protected
void
GridView1_RowDataBound(
object
sender, GridViewRowEventArgs e)
... {
if (e.Row.RowIndex != -1)
...{
int id = e.Row.RowIndex + 1;
e.Row.Cells[0].Text = id.ToString();
}
}
... {
if (e.Row.RowIndex != -1)
...{
int id = e.Row.RowIndex + 1;
e.Row.Cells[0].Text = id.ToString();
}
}
4.隐藏某一列,取得该列的值。有两种办法,第一,直接从gridview的datasource里取得,需要在databind之后,第二,可以将该列设为datakeys,如:gridview1.DataKeyNames = new string[] { "D" }; 取值时:string ID = gridview1.DataKeys[0].Value.ToString();
5.使用TemplateField时,取得控件的值:
DropDownList ddl;
ddl = (DropDownList)GridView2.Rows[i].FindControl( " DropDownList1 " );
ddl = (DropDownList)GridView2.Rows[i].FindControl( " DropDownList1 " );
主要转载于:http://xiahengzhu.spaces.live.com/blog/cns!BB1C3B263778DC06!126.entry