gridview knowledge

深入挖掘ASP.NET 2.0系列课程(1):随心所欲的使用GridView
2008-02-17 22:34

视频的DEMO是用的VS2005,用VS2008照着上面说的做了下,没有什么问题。这里就把里面相关的一些代码粘上来,以后有用到时候,可以直接COPY下。也算是自己学习的一个笔记。有一些代码很长,我自己也没有做试例,我这里也不做了,等做了有代码,我再把他COPY上来。

1,GridView里面,鼠标放上去行会变颜色,鼠标也会变成手形状。

    protected void GridView1_RowCreated1(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Attributes.Add("onmouseover", "currentcolor=this.style.backgroundColor;this.style.backgroundColor='#A9A9A9';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[2].Text == "0")
                e.Row.Cells[2].BackColor = System.Drawing.Color.Red;
        }
    }

3,表头的CheckBox打勾,所有单元格CeckBox都打上勾。选中GridView,编辑列,选中TemplateField,添加,选中GridView,编辑模板列,在ItemTemplate,HeaderTemplate,各装入一CheckBox,双击HeaderTemplate 里面CheckBox,并设置为AutoPostBack为True,事件代码如下,

    protected void CheckBox3_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;
            }
        }
    }

4,行点击删除的时候,跳出确定要删除?对话框。选中GridView,编辑列,CommandField,删除,点,添加,将此字段转换为TemplateField,编辑删除模板列,选中LinkButton,在属性,OnClientClick中,加入代码:return confirm("确定要删除吗?"),OK,

5,还有一个GridView导出Excel,我没有在VS2008里面测试,也没有手写一句一句的代码,我只是在网上查了,感觉他们写得都很好,我现在把它粘在这里,以后写的时候,我再把代码换成最新的,

导入excel 注意:1:格式化导入excel 里面的数据 2:Response.Clear();据说没有这会导致乱码
protected void Button1_Click(object sender, EventArgs e)
{
Response.Clear();
Response.Buffer = true;
Response.Charset = "GB2312";
Response.AppendHeader("Content-Disposition", "attachment;filename=FileName.xls");
Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");//设置输出流为简体中文
Response.ContentType = "application/ms-excel";//设置输出文件类型为excel文件。
this.EnableViewState = false;
System.Globalization.CultureInfo myCItrad = new System.Globalization.CultureInfo("ZH-CN", true);
System.IO.StringWriter oStringWriter = new System.IO.StringWriter(myCItrad);
System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
this.DataGrid1.RenderControl(oHtmlTextWriter);
Response.Write(oStringWriter.ToString());
Response.End();
}


1:

 

protected void DataGrid1_ItemDataBound(object sender, DataGridItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
e.Item.Cells[0].Attributes.Add("style", "vnd.ms-excel.numberformat:@");
e.Item.Cells[3].Attributes.Add("style", "vnd.ms-excel.numberformat:¥#,###.00");
}
}

关健是这两句啦

e.Item.Cells[0].Attributes.Add("style", "vnd.ms-excel.numberformat:@");//格式字符串比如080001没有这句话就会变成80001
e.Item.Cells[3].Attributes.Add("style", "vnd.ms-excel.numberformat:¥#,###.00");


还有其它

首先,我们了解一下excel从web页面上导出的原理。当我们把这些数据发送到客户端时,我们想让客户端程序(浏览器)以excel的格式读取它,所以把mime类型设为:application/vnd.ms-excel,当excel读取文件时会以每个cell的格式呈现数据,如果cell没有规定的格式,则excel会以默认的格式去呈现该cell的数据。这样就给我们提供了自定义数据格式的空间,当然我们必须使用excel支持的格式。下面就列出常用的一些格式:
1) 文本:vnd.ms-excel.numberformat:@
2) 日期:vnd.ms-excel.numberformat:yyyy/mm/dd
3) 数字:vnd.ms-excel.numberformat:#,##0.00
4) 货币:vnd.ms-excel.numberformat:¥#,##0.00
5) 百分比:vnd.ms-excel.numberformat: #0.00%

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值