您的位置:首页 > 其它

GridView使用技巧小结

2009-11-13 09:49 302 查看
①添加鼠标移动事件
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");
}
}

②添加双击事件
【客户端】
function ReKey(k)
{
alert("姓名"+k);
}
【服务器端】
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+"')");
}
}

 ③添加键盘事件
【客户端】
<mce:script language=javascript><!--
function GridViewItemKeyDownEvent(d)
{
window.alert("事件类型: GridViewItemKeyDownEvent  作用对象:" + d);
}
// --></mce:script>
【服务器端】
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType==DataControlRowType.DataRow)
{
//键盘事件
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)
{
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 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;
}
}
}

  ⑥添加删除确认效果
<asp:TemplateField HeaderText="删除" ShowHeader="False">
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" CommandName="Delete" OnClientClick='return confirm("确认要删除吗?")' Text="删除"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>

⑦导出到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();
}

⑧不使用数据源控件的GridView。(只显示标题,不显示内容)
思路:添加一空行;
private void AddDummyData(DataSet ds)
{
// Add a dummy row
DataTable dt = ds.Tables[0];
DataRow newRow = dt.NewRow();
dt.Rows.Add(newRow);
}
 
 
 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: