1,给GridView加上鼠标移动改变背景色效果:
增加GridView的RowCreated(很明显是当GridView行创建)事件代码
这里this.style.backgroundColor='#C0C0FF' 的值可以是这样的十六进制也可以直接是颜色名称如='red'
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");
}
}
2: 判断特定条件,更改单元格背景颜色:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
...{ //数据加载时发生
if (e.Row.RowType == DataControlRowType.DataRow)
...{ //判断是否是数据行
if (e.Row.Cells[8].Text == "USA")
...{ //判断第9列值是否为"USA"
//e.Row.BackColor = System.Drawing.Color.Red;
e.Row.Cells[8].BackColor = System.Drawing.Color.Red;
}
}
}3:在父窗体中调用子窗体的gridview值(简单的页面交互):
父窗体代码:
注:单机事件用window.open打开新窗体并获得焦点
<head>
<script language=javascript>...
function openpage(htmlurl) 
...{
var newwin=window.open(htmlurl,"newWin","toolbar=no,location=no,directories=no,status=no,scrollbars=yes,menubar=no,resizable=yes,top=100,left=200,width=650,height=300");
newwin.focus();
return false;
}
</script>
</head>
<body>
<input type=text id="name" />
在按钮中调用:
<input type=button value="调用" onclick="return openpage('GridViewClientClick.aspx');" />
</body>子窗体代码:
注:girdview中e.Row.Attributes增加单击属性,ReKey并将此行第三列的值传过去
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["style"] = "Cursor:hand";
// //键盘事件
//e.Row.Attributes.Add("OnKeyDown", "GridViewItemKeyDownEvent('" + e.Row.Cells[1].Text + "')");
}
}其中rekey的前台的JS脚本:
注: window.opener是获得该子窗体的父窗体对象getElementById('name').value=k;是把参数值传给父窗体的name对象
<script language=javascript>
function GridViewItemKeyDownEvent(d)
...{
window.alert("事件类型: GridViewItemKeyDownEvent 作用对象: " + d);
}
function ReKey(k)
...{
window.opener.document.getElementById('name').value=k;
window.close();
}
</script>4:GirdView增加全选列,单击checkbox全选此列并删除
首先添加一个模版列,在header和item模版中添加checkbox.它的代码如下:注意要把:CheckBox2的 Autopostback属性设为true
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 Button2_Click(object sender, EventArgs e)
...{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["girdviewConnectionString"].ConnectionString);
con.Open();
for (int i = 0; i < GridView1.Rows.Count; i++)
...{
if (((CheckBox)GridView1.Rows[i].FindControl("CheckBox1")).Checked == true)
...{
int id=Convert.ToInt32(GridView1.DataKeys[i]["user_id"]);
SqlCommand cmd = new SqlCommand("delete from [user] where user_id='" + id + "'", con);
cmd.ExecuteNonQuery();
GridView1.DataBind();
}
}
}
5:为删除增加提示效果:
很简单,发个图片就可以明白了----前提是把"删除"列转化为模版列,代码就一句: return confirm("确定删除吗?")
6:将表格倒出为Excel:
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);
this.GridView1.RenderControl(oHtmlTextWriter);
Response.Output.Write(oStringWriter.ToString());
Response.Flush();
Response.End();
}

495

被折叠的 条评论
为什么被折叠?



