gridview常用技巧

1. 为按钮增加确认对话框。在RowDataBound事件中,为按钮增加"onclick"事件属性
  protected   void  gvVersionList_RowDataBound( object  sender, GridViewRowEventArgs e)
    
{
        
//如果是绑定数据行 
        if (e.Row.RowType == DataControlRowType.DataRow)
        
{
            
if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate)
            
{
                ((LinkButton)e.Row.Cells[
6].Controls[0]).Attributes.Add("onclick""javascript:return confirm('你确认要删除:"" + e.Row.Cells[1].Text + ""吗?')");
            }

        }

    }

2.鼠标移到GridView某一行时改变该行的背景色。和1的做法基本一样,在在RowDataBound事件中,为按钮增加"onmouseover"事件和"onmouseout"事件属性

protected   void  GridView1_RowDataBound( object  sender, GridViewRowEventArgs e)
    
{
        
//如果是绑定数据行 
        if (e.Row.RowType == DataControlRowType.DataRow)
        
{
            
//鼠标经过时,行背景色变 
            e.Row.Attributes.Add("onmouseover""this.style.backgroundColor='#E6F5FA'");
            
//鼠标移出时,行背景色变 
            e.Row.Attributes.Add("onmouseout""this.style.backgroundColor='#FFFFFF'");
        }
 

    }
 

3.为每一行增加一个行号

    protected   void  GridView1_RowDataBound( object  sender, GridViewRowEventArgs e)
    
{
        
if (e.Row.RowIndex != -1)
        
{
            
int id = e.Row.RowIndex + 1;
            e.Row.Cells[
0].Text = id.ToString();
        }

    }

4.隐藏某一列,取得该列的值。有两种办法,第一,直接从gridview的datasource里取得,需要在databind之后,第二,可以将该列设为datakeys,如:gridview1.DataKeyNames = new string[] { "D" }; 取值时:string ID = gridview1.DataKeys[0].Value.ToString();

5.使用TemplateField时,取得控件的值:

DropDownList ddl;
        ddl 
=  (DropDownList)GridView2.Rows[i].FindControl( " DropDownList1 " );

主要转载于:http://xiahengzhu.spaces.live.com/blog/cns!BB1C3B263778DC06!126.entry

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值