GridView method to get the current row index

Source: Internet
Author: User

When using the GridView control, we often run into indexes that fetch the current row and perform many operations through the index. For example, you can get a control element for the current row, set the value of an element, and so on.

Here are some examples of ways to get the index value of the current GridView row.

Instance:

① purpose: Gets the current index row of Rowcommand in the GridView.

② Front Page: Add a template column to the GridView and add a LinkButton control inside it.

Code:

<asp:templatefield headertext= "Operation" >

<ItemTemplate>

<asp:linkbutton id= "Lbtnqianru" runat= "server" commandname= "Qianru"

Commandargument= ' <%# Eval ("Id")%> ' > Check in </asp:LinkButton>

<asp:linkbutton id= "Lbtnqianchu" runat= "Server" commandname= "Qianchu" > Checkout </ASP:LINKBUTTON&G T

</ItemTemplate>

</asp:TemplateField>

Tip : If e.commandargument is used in the background code, the foreground code must set the value of CommandArgument in the button, which is the bound database field. Such as:

Because the LinkButton CommandArgument is bound to the primary key ID in the client, the value of the primary key ID can be derived directly from the e.commandargument.

int id = convert.toint32 (e.commandargument.tostring ());

③ has set the LinkButton as the event handling button in the GridView, the index will be obtained by the following methods:

protected void Gv_company_rowcommand (object sender, Gridviewcommandeventargs e) {

if (E.commandname = = "Qianru")

{

"Method One"

GridViewRow DRV = ((GridViewRow) (((LinkButton) (E.commandsource)). Parent.parent)); The resulting value is the index value that represents the row being selected

INF Id=convert.toint32 (GridView1. DataKeys[DRV. RowIndex]. Value); This gets the value of the primary key in the database that is bound in the GridView

Note: using this method, you need to set the DataKeyNames property of the GridView, which is set as the primary key field in this example.

"Method Two"

GridViewRow DRV = (GridViewRow) ((LinkButton) e.commandsource). namingcontainer;//This value is the index value that represents the row being selected

int id = convert.toint32 (gridview1.rows[drv. RowIndex]. Cells[0].   Text); This gets the value of the primary key value in the database that is bound in the GridView, and the value method is the value of the first column in the selected row , DRV. RowIndex gets the index of the selected row

}

}

In addition , there are ways to achieve the current row index value.

The "Method three" command event in the LinkButton control takes advantage of the parent of sender to get the current row in the GridView.

protected void Lbtnqianchu_command (object sender, CommandEventArgs e)

{

LinkButton lb = (LinkButton) sender;

DataControlFieldCell DCF = (DataControlFieldCell) lb. Parent;

GridViewRow GVR = (gridviewrow) DCF. Parent; The resulting value is the index value that represents the row being selected

Lbtnqianchu.selectedindex = Gvr. RowIndex;

}

"Method Four" in the LinkButton control's Click event, gets the current row in the GridView.

protected void LinkButton1_Click (object sender, EventArgs e)

{

Line number

int row = ((GridViewRow) ((LinkButton)sender). NamingContainer). RowIndex;

}

"Method Five" if you add the DropDownList control in the template column and open its AutoPostBack property, in the SelectedIndexChanged event of DropDownList, get the current row in the GridView.

The following is a code summary of the selectedindexchanged event:

DropDownList DDL = (DropDownList)sender;

GridViewRow GVR = (gridviewrow) DDL. NamingContainer;

int id = Int. Parse (GRIDVIEW1.DATAKEYS[GVR. Rowindex][0]. ToString ());

int num = Int. Parse (DDL. Text);

The first sentence is used to get the DropDownList control that triggers the event.

The second sentence takes advantage of the NamingContainer property of the control to get its container, which is the GridViewRow object.

tip: because dropdowelist cannot specify its commandname because it differs from button, the problem is resolved by using the NamingContainer property.

Let's take a look at Microsoft's explanation of the NamingContainer Property :

Gets a reference to the naming container for a server control that creates a unique namespace to differentiate between server controls that have the same Control.id property value.

Each page of an ASP. NET WEB application contains a hierarchical structure of controls. This hierarchy is independent of whether the control generates user-visible UI. The naming container for a given control is the parent control on top of the control in the hierarchy, which implements the INamingContainer interface. The server control that implements this interface creates a unique namespace for the ID property value of its child server control.

Creating a unique namespace for a server control is especially important when data binding against list Web server controls, such as Repeater and DataList server controls. When multiple items in a data source create multiple instances of a server control, and the server control is a child of a repeating control, the naming container ensures that each instance of these child controls has a non-conflicting UniqueID property value. The default naming container for the page is an instance of the page class that is generated when you request it.

You can use this property to determine the naming container in which a particular server control resides.

"Method Six" if there is a CheckBox control in the template column, get the current row in the GridView through the checkbox1_checkedchanged event.

CheckBox chk = (checkbox)sender;

DataControlFieldCell DCF = (DataControlFieldCell) chk. Parent;

GridViewRow GVR = (gridviewrow) DCF. Parent;

"Method Seven"

<asp:gridview id= "gvtest" runat= "Server" >

<Columns>

<asp:TemplateField>

<ItemTemplate>

DisplayIndex: <%# container.displayindex%> | | Dataitemindex: <%# container.dataitemindex%><br/>

</ItemTemplate>

</asp:TemplateField>

</Columns>

</asp:GridView>

GridView Gets the current row index method

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.