Summary of methods for obtaining row indexes in the Rowcommand of asp.net gridview

Source: Internet
Author: User

1. Obtain the index of the current row through the command source.

There are many methods, and rows cannot be directly obtained in the Command event of the GridView as in the DataGrid,

Method 1,
GridViewRow drv = (GridViewRow) (Button) (e. commandSource )). parent. parent); // CommandSource causes the event's command source. (in doubt, according to MSDN, It is a GridView. If so, this operation is incorrect, but what I get is indeed correct, it indicates that the BUtton control is obtained and will be verified later ).
Drv. RowIndex


2. Bind the row index to the CommandArgument of the control in the RowDataBound event

Because the event parameter GridViewCommandEventArgs does not disclose the Row attribute to indicate the current Row, (DataGridCommandEventArgs exposes the Item attribute to obtain the DataGridItem of course, I do not know ASP. NET Team), so we need a "trick" to get this property.

In fact, this is a long-known issue. In view of every doubt in CSDN, we will sort it out here to facilitate the following:

Copy codeThe Code is as follows:
Protected void GridView1_RowCommand (object sender, GridViewCommandEventArgs e)
{
Int rowIndex =-1;
GridViewRow row = null;
Switch (e. CommandName )...{
Case "Command1": // template Column
// For buttons in the template column, we need to display the CommandArgument attribute of the bound row index to the button
// Obtain the row information of the trigger event
RowIndex = Convert. ToInt32 (e. CommandArgument );
Row = GridView1.Rows [rowIndex];
DisplayInfo (row, e. CommandName );
// Your codes
//
Break;
Case "Command2": // template Column
// It is also in the template column, but does not use the Command1 method. Instead, it uses the NamingContrainer attribute.
// Directly obtain the current GridViewRow
Control primitive Control = e. CommandSource as Control; // It indicates the IButtonControl that triggers the event to maintain uniformity and facilitate subsequent operations. Here we directly convert it to the Control base class Control.
Row = lateral control. NamingContainer as GridViewRow;
DisplayInfo (row, e. CommandName );
// Your codes
//
Break;
Case "Command3": // bind a column
// For the ButtonField column, the data source control automatically fills the CommandArgument attribute with the appropriate item index value.
// Without the need for us to bind its CommandArgument attribute
// Note: we cannot use Command2 here. For events triggered by BUttonField,
// The GridView containing this button in GridViewCommandEventArgs. CommandSource
RowIndex = Convert. ToInt32 (e. CommandArgument );
Row = GridView1.Rows [rowIndex];
DisplayInfo (row, e. CommandName );
// Your codes
//
Break;
}
}

Related Article

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.