Asp. NET uses the GridView to get the index value of the current line _ Practical tips

Source: Internet
Author: User

When using the GridView control, we often encounter the index of getting the current row and 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. The following examples describe several methods for obtaining the current row index values of the GridView.

Instance:
① purpose: Gets the current index row of Rowcommand in the GridView.
② Foreground page: Add a template column to the GridView, adding a LinkButton control.
Code:

<asp:templatefield headertext= "Operations" > 
<ItemTemplate> 
<asp:linkbutton id= "Lbtnqianru" Server "Commandname=" Qianru " 
commandargument=" <%# Eval ("Id")%> ' > check-in </asp:LinkButton> 
< Asp:linkbutton id= "Lbtnqianchu" runat= "Server" commandname= "Qianchu" > Checkout </asp:LinkButton> 
</ Itemtemplate> 
</asp:TemplateField> 

Tip: If you use e.commandargument in the background code, the foreground code must set the CommandArgument value in the button, and the value is the bound database field. Such as:
Because the LinkButton CommandArgument is already bound to the primary key ID in the client, you can use E.commandargument to derive the value of the primary key ID directly here
int id = Convert.ToInt32 (e.commandargument.tostring ());
③ is already set up in the GridView LinkButton for the event-handling button, the index is 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 
inf Id=convert.toint32 (GRIDVIEW1.DATAKEYS[DRV) that represents the row being selected. RowIndex]. Value); This gets the value of the primary key in the bound database 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.

"Method Two"

GridViewRow DRV = (GridViewRow) ((LinkButton) e.commandsource). namingcontainer;//This is the value that represents the selected index value 
int id = convert.toint32 (gridview1.rows[drv). RowIndex]. Cells[0]. Text); This gets the value of the primary key value in the bound database in the GridView, which is the value of the first column in the selected row, DRV. RowIndex gets the index of the selected row} 
} 

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

"Method Three"

In the command event for the LinkButton control, use the sender parent 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 value that is derived is the index value Lbtnqianchu.selectedindex = GVR that the row is selected 
. RowIndex; 
} 

"Method Four"

Gets the current row in the GridView in the LinkButton control's Click event.

protected void LinkButton1_Click (object sender, EventArgs e) 
{ 
//line number 
int row = ((GridViewRow) (LinkButton ) sender). NamingContainer). RowIndex; 
} 

"Method V"

If you add a DropDownList control to the template column and open its AutoPostBack property, in the DropDownList SelectedIndexChanged event, get the current row in the GridView.

The following is a summary of the code for 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 obtain its container, which is the GridViewRow object.
Tip: Because the dropdowelist is different from the button, you cannot specify its commandname, so you can solve the problem by using the NamingContainer property.
Let's take a look at Microsoft's explanation of the NamingContainer attribute:
Gets a reference to the server control's naming container, which creates a unique namespace to differentiate between server controls with the same Control.id property values.
Each page of the ASP.net Web application contains a hierarchy of controls. This hierarchy has nothing to do with whether the control generates a 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 server controls is especially important when data binding is performed 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 those child controls has a UniqueID property value that does not conflict. The default naming container for a 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 where 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; 

"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> 

"Method Eight"

The ID and name naming of controls can be as above, and I need to use the Rowcommand () method to determine which column is selected, and the premise of using this method is to e.commandargument such a property (first you must know that in the GridView, The row index is placed inside the commandargument, and now the task is to get such a property. As you can see, when you create each row in the GridView control, a rowcreated event is raised that allows you to write the line number selected by LinkButton in CommandArgument.

protected void gvinfo_rowcreated (object sender, GridViewRowEventArgs e) 
{ 
if (E.row.rowtype = = Datacontrolrowtype.datarow) 
{ 
LinkButton lk1 = (LinkButton) e.row.findcontrol ("lkbtn"); ID of//linkbutton 
lk1.commandargument = e.row.rowindex.tostring (); 
} 
} 
protected void Gvinfo_rowcommand (object sender, Gridviewcommandeventargs e) 
{ 
if (e.commandname = "ADD")// I LinkButton commandname 
{ 
int index = Convert.ToInt32 (e.commandargument); 
String AA = Gvinfo.rows[index]. CELLS[1]. Text.tostring ()//Get the current row number of a value, column number starting from 0 
} 
 

The above mentioned is the entire content of this article, I hope you can enjoy.

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.