GridView事件中获取rowIndex值

其实可以用存储过程来实现.在表中加一个sequence(排序)字段.
CREATE PROCEDURE [dbo].[sp_MoveUp_contactinfo]   --向上移动.向下移动跟这差不多.
(
@pkey bigint 
)
AS
BEGIN
declare @sequence as int
select @sequence = sequence from tb_contact_info where 主键 = @pkey

declare @Dpkey as bigint


select @Dpkey = 主键 from 表名 where  sequence = ( select max(sequence) from  表名 where sequence < @sequence)

if exists(select top 1 sequence from 表名 where  sequence < @sequence) 
begin
update tb_contact_info set sequence = (select top 1 sequence from tb_contact_info as a where a.主键 = @Dpkey) where 主键= @pkey;

update 表名 set sequence = sequence + 1 where 主键 = @Dpkey ;
end
END

在GridView中添加两列模板列.里面放两个按钮.在RowCommand事件中写下面的代码.
C# codestring type = ""; ImageButton btn = e.CommandSource as ImageButton; if (btn.ID == "moveDown") { type = "up"; } if (btn.ID == "moveUp") { type = "down"; } GridViewRow row = btn.NamingContainer as GridViewRow; string pkey = dgv_Membership.DataKeys[row.RowIndex].Value.ToString(); string contenttype = row.Cells[5].Text.Replace(" ", "").Trim(); //这里调用执行存储过程的方法 if (errMsg != "") { Response.Write(clsCommon.Msg("Errors:" + errMsg)); }    //这里再绑定一次.

在RowDataBound事件中写
C# code protected void grvNews_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { if (e.Row.RowIndex == 0) { e.Row.FindControl("moveUp").Visible = false; } if (e.Row.DataItemIndex == (dgv_Membership.DataSource as DataView).Count - 1) { e.Row.FindControl("moveDown").Visible = false; }     } }
原文地址:https://www.cnblogs.com/huige1004/p/1305742.html