玩转GridView

(1)根据内容设置GridView某些行,或单元格的颜色。

 

ContractedBlock.gifExpandedBlockStart.gifCode
    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复选框实现全部选择。

 

ContractedBlock.gifExpandedBlockStart.gifCode
   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)进行重载。

 

ContractedBlock.gifExpandedBlockStart.gifCode
   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某行时,动态改变该行的背景颜色。

 

ContractedBlock.gifExpandedBlockStart.gifCode
    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响应单击或双击行事件。

 

ContractedBlock.gifExpandedBlockStart.gifCode
    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("确认要删除吗?")'

转载于:https://www.cnblogs.com/carekee/articles/1723999.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值