GridView的RowCommand事件中取得行索引

简介:     protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)    ...{        if (e.
     protected   void  GridView1_RowDataBound( object  sender, GridViewRowEventArgs e)
    
... {
        
if (e.Row.RowType == DataControlRowType.DataRow)//如果是为数据行
        ...{
            ImageButton imgbtnup 
= (ImageButton)e.Row.Cells[1].FindControl("btnMoveUp");//找控件
            imgbtnup.CommandArgument = e.Row.RowIndex.ToString();//设置与此BUTTON关联的命令参数
            imgbtnup.Visible = e.Row.RowIndex != 0
            ImageButton imgbtndown 
= (ImageButton)e.Row.Cells[2].FindControl("btnMoveDown");
            imgbtndown.CommandArgument 
= e.Row.RowIndex.ToString();
            imgbtndown.Visible 
= e.Row.RowIndex != ((DataSet)((GridView)sender).DataSource).Tables[0].Rows.Count - 1;
        }

    }


    
protected   void  GridView1_RowCommand( object  sender, GridViewCommandEventArgs e)
    
... {
        
if (e.CommandName == "MoveUp")
        
...{
            
int index = Convert.ToInt32(e.CommandArgument);//取的行索引
            DataKey key = this.GridView1.DataKeys[index];
            
string keyval = key.Value;//取得主键
        }

        
else if (e.CommandName == "MoveDown")
        
...{
            
int index = Convert.ToInt32(e.CommandArgument);
            DataKey key 
= this.GridView1.DataKeys[index];
            
string keyval = key.Value; 
        }

    }




文章出处:http://blog.csdn.net/sonce8/archive/2007/09/09/1777777.aspx

目录
相关文章
C#编程-77:DataGridView绘制行序号
C#编程-77:DataGridView绘制行序号
182 0
C#编程-77:DataGridView绘制行序号
|
Web App开发 Java Android开发
列表ListBox、ListView、GridView 排序
列表排序 1.使用控件默认排序方式(推荐) ListControl.Items.SortDescriptions.Clear(); ListControl.Items.SortDescriptions.
872 0
DevExpress GridView 列标题点击事件
GridView有RowCellClick事件,即单元格点击事件,但是针对列标题行以及列标题单元格却没有相应的事件。 在这里使用GridView的MouseDown事件。这里同样使用的是GridHitInfo来获取点击位置的信息,来判断是否在列标题上。GridHitInfo根据鼠标点击的x、y坐标获取该点的相关信息,判断是否点击在列标题行内。 private void
1643 0
|
.NET 开发框架 数据库
GridView获取隐藏列的值
最近试着看了一下Asp.Net,以前开发过一些Web的系统,但是后来一直没在搞Web的了,所以Web方面的知识都忘记的差不多了,现在遇到一些问题就记下来,以便日后查看。在GridView里面获取隐藏列的值,有时候一些Key字段不想让用户看见,但是后台又需要使用这个关键字段去做一些操作,如果使用普通的...
808 0
|
.NET
GridView,Repeater增加自动序号列
有三种实现的方式, 第一种方式,直接在Aspx页面GridView模板列中.这种的缺点是到第二页分页时又重新开始了. 第二种方式分页时进行了计算,这样会累计向下加.
963 0
gridview 中SelectedIndexChanged 事件获得该行主键
protected void gvSuppliers_SelectedIndexChanged(object sender, EventArgs e)        {            lblCompany.
706 0