(1)根据内容设置GridView某些行,或单元格的颜色。
Code
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (e.Row.Cells[8].Text == "USA")
{
e.Row.BackColor = System.Drawing.Color.Red;
//e.Row.Cells[8].BackColor = System.Drawing.Color.Red;
}
}
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (e.Row.Cells[8].Text == "USA")
{
e.Row.BackColor = System.Drawing.Color.Red;
//e.Row.Cells[8].BackColor = System.Drawing.Color.Red;
}
}
}
(2)GridView复选框实现全部选择。
Code
protected void CheckBox2_CheckedChanged(object sender, EventArgs e)
{
int i;
if (((CheckBox)sender).Checked)
{
for (i = 0; i < GridView1.Rows.Count; i++)
{
((CheckBox)GridView1.Rows[i].FindControl("CheckBox1")).Checked = true;
}
}
else
{
for (i = 0; i < GridView1.Rows.Count; i++)
{
((CheckBox)GridView1.Rows[i].FindControl("CheckBox1")).Checked =false;
}
}
}
protected void CheckBox2_CheckedChanged(object sender, EventArgs e)
{
int i;
if (((CheckBox)sender).Checked)
{
for (i = 0; i < GridView1.Rows.Count; i++)
{
((CheckBox)GridView1.Rows[i].FindControl("CheckBox1")).Checked = true;
}
}
else
{
for (i = 0; i < GridView1.Rows.Count; i++)
{
((CheckBox)GridView1.Rows[i].FindControl("CheckBox1")).Checked =false;
}
}
}
(3)GridView导出表到excel文件。
提供了对分页的支持,如果需要屏敝某些列,将这些列的Visible属性设为false即可。需要注意的是,一定要对方法VerifyRenderingInServerForm(Control control)进行重载。
Code
protected void Button1_Click(object sender, EventArgs e)
{
Response.Clear();
Response.Buffer = true;
Response.Charset = "GB2312";
Response.AppendHeader("Content-Disposition", "attachment;filename=FileName.xls");
// 如果设置为 GetEncoding("GB2312");导出的文件将会出现乱码!!!
Response.ContentEncoding = System.Text.Encoding.UTF7;
Response.ContentType = "application/ms-excel";//设置输出文件类型为excel文件。
System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
bool bPage = GridView1.AllowPaging;
if (bPage)
{
GridView1.AllowPaging = false;
GridView1.DataBind();
}
this.GridView1.RenderControl(oHtmlTextWriter);
Response.Output.Write(oStringWriter.ToString());
Response.Flush();
Response.End();
if (bPage)
{
GridView1.AllowPaging = true;
GridView1.DataBind();
}
}
public override void VerifyRenderingInServerForm(Control control)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
Response.Clear();
Response.Buffer = true;
Response.Charset = "GB2312";
Response.AppendHeader("Content-Disposition", "attachment;filename=FileName.xls");
// 如果设置为 GetEncoding("GB2312");导出的文件将会出现乱码!!!
Response.ContentEncoding = System.Text.Encoding.UTF7;
Response.ContentType = "application/ms-excel";//设置输出文件类型为excel文件。
System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
bool bPage = GridView1.AllowPaging;
if (bPage)
{
GridView1.AllowPaging = false;
GridView1.DataBind();
}
this.GridView1.RenderControl(oHtmlTextWriter);
Response.Output.Write(oStringWriter.ToString());
Response.Flush();
Response.End();
if (bPage)
{
GridView1.AllowPaging = true;
GridView1.DataBind();
}
}
public override void VerifyRenderingInServerForm(Control control)
{
}
(4)鼠标移到GridView某行时,动态改变该行的背景颜色。
Code
protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow )
{
e.Row.Attributes.Add("onmouseover", "currentcolor=this.style.backgroundColor;this.style.backgroundColor='#C0C0FF';this.style.cursor='hand';");
//当鼠标移走时还原该行的背景色
e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=currentcolor");
}
}
protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow )
{
e.Row.Attributes.Add("onmouseover", "currentcolor=this.style.backgroundColor;this.style.backgroundColor='#C0C0FF';this.style.cursor='hand';");
//当鼠标移走时还原该行的背景色
e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=currentcolor");
}
}
(5)GridView响应单击或双击行事件。
Code
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType==DataControlRowType.DataRow)
{
//e.Row.Attributes.Add("ondblclick", "ReKey('" + e.Row.Cells[2].Text+"')");
//e.Row.Attributes.Add("onclick", "alert('" + e.Row.Cells[2].Text + "')");
e.Row.Attributes.Add("ondblclick", "window.open('http://www.sina.com?name=" + e.Row.Cells[2].Text + "')");
e.Row.Attributes["style"] = "Cursor:hand";
// //键盘事件
//e.Row.Attributes.Add("OnKeyDown", "GridViewItemKeyDownEvent('" + e.Row.Cells[1].Text + "')");
}
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType==DataControlRowType.DataRow)
{
//e.Row.Attributes.Add("ondblclick", "ReKey('" + e.Row.Cells[2].Text+"')");
//e.Row.Attributes.Add("onclick", "alert('" + e.Row.Cells[2].Text + "')");
e.Row.Attributes.Add("ondblclick", "window.open('http://www.sina.com?name=" + e.Row.Cells[2].Text + "')");
e.Row.Attributes["style"] = "Cursor:hand";
// //键盘事件
//e.Row.Attributes.Add("OnKeyDown", "GridViewItemKeyDownEvent('" + e.Row.Cells[1].Text + "')");
}
}
(6)GridView添加删除确认。
将原有的删除列转化为模板列,在页面上添加确认代码:
OnClientClick='return confirm("确认要删除吗?")'