Obtain the rowIndex value in the GridView event

Source: Internet
Author: User
You can actually use a stored procedure to add a sequence (SORT) field to the table.
Create procedure [dbo]. [sp_MoveUp_contactinfo] -- move up. Move down similar to this.
(
@ Pkey bigint
)
AS
BEGIN
Declare @ sequence as int
Select @ sequence = sequence from tb_contact_info where primary key = @ pkey

Declare @ Dpkey as bigint

Select @ Dpkey = primary key from table name where sequence = (select max (sequence) from table name where sequence & lt; @ sequence)

If exists (select top 1 sequence from table name where sequence & lt; @ sequence)
Begin
Update tb_contact_info set sequence = (select top 1 sequence from tb_contact_info as a where a. Primary Key = @ Dpkey) where primary key = @ pkey;

Update table name set sequence = sequence + 1 where primary key = @ Dpkey;
End
END

Add two template columns in the GridView. Put two buttons in it. Write the following code in the RowCommand event.
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 ("& nbsp ;",""). trim (); // The method for executing the stored procedure is called here if (errMsg! = "") {Response. Write (clsCommon. Msg ("Errors:" + errMsg);} // bind it again here.

Write in RowDataBound event
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 ;}}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.