GridView一些常用

1)GridView中加载bool类型的sex显示问题

 <%#bool.Parse(Eval("Sex").ToString()) == true ? "" : "" %>

2):利用普通checkbox 控件来制作全选功能,删除时的代码

string deleteStr = Request["group1"]; //获取选中的checkbox集合

SqlCommand cmd = new SqlCommand("delete from ShoppingCart where productId in (" + deleteStr + ")", conn);//注意此处不能用

cmd.Parameters.AddWithValue("@deleteStr", deleteStr);

//前提是先绑定 checkbox value

<input id="Checkbox1" type="checkbox" name="group1" value='<%#Eval("productId") %>' />

实现全选Javascript代码: 有checkBox2调用此方法

function getCheckAll(chk)

       {

           var items=document.getElementsByTagName("Input");

           for(i=0;i<items.length;i++)

           {

              if(items[i].type=="checkbox")

                  items[i].checked=chk.checked;

           }

       }

3):实现光棒效果与合并单元格

protected void gvMain_RowCreated(object sender, GridViewRowEventArgs e)

{

    //实现光棒效果

        if (e.Row.RowType == DataControlRowType.DataRow)//给行添加事件

        {

            e.Row.Attributes.Add("onmouseover","this.style.backgroundColor='#6699ff'");

            e.Row.Attributes.Add("onmouseout","this.style.backgroundColor=''");

        }

        //合并单元格

        else if (e.Row.RowType == DataControlRowType.Footer)//给页脚添加内容

        {

            int count = e.Row.Cells.Count;//获得页脚的例数

            for (int i = count-2;i>0; i--)//不删除第一格和最后一格

            {

                e.Row.Cells.RemoveAt(i);

            }

            //合并单元格,把除最后一格的weight加到第一格

            e.Row.Cells[0].ColumnSpan = count - 1;

            //为第一格设置文本对齐方式

            e.Row.Cells[0].Style.Add("text-align", "left");

            //用于显示总金额

Literal lblTotal = new Literal();

            lblTotal.ID = "lblTotal";

            lblTotal.Text ="总金额"+ string.Format("{0:C}",(BLL.ShoppingCartManager.SelectTotal()));

            int newCount = e.Row.Cells.Count;

            e.Row.Cells[newCount-1].Controls.Add(lblTotal);

            e.Row.Cells[newCount-1].Style.Add("text-align", "right");

 

        }

}

4:) 把GridView控件中的数据导入到Excel中去

1,  首先要把GridView页面 EnableEventValidation="false",对应按钮的Click事件加上如下代码:

        Response.Clear();

        Response.Buffer = true;

        Response.Charset = "GB2312";

        Response.AppendHeader("Content-Disposition", "attachment;filename="+FileName);//FileName为文件下载名称

        // 如果设置为 GetEncoding("GB2312");导出的文件将会出现乱码!!!

        Response.ContentEncoding = System.Text.Encoding.UTF7;

        Response.ContentType = FileType;//设置输出文件类型为excel文件。

        System.IO.StringWriter oStringWriter = new System.IO.StringWriter();

        System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);

        this.gvMain.RenderControl(oHtmlTextWriter);

        Response.Output.Write(oStringWriter.ToString());

        Response.Flush();

        Response.End();

  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值