This article mainly introduced asp.net use GridView to get the index value of the current row rollup, the need for small partners can refer to.
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:
?
1 2 3 4 5 6 7 |
<asp:templatefield headertext= "Operations" > <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> </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:
?
1 2 3 4 |
protected void Gv_company_rowcommand (object sender, Gridviewcommandeventargs e) {if (E.commandname = "Qianru") { |
"Method One"
?
1 2 |
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"
?
1 2 3 4 |
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.
?
1 2 3 4 5 6 7 |
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.
?
1 2 3 4 5 |
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:
?
1 2 3 4 |
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.
?
1 2 3 |
CheckBox chk = (checkbox) sender; DataControlFieldCell DCF = (DataControlFieldCell) chk. Parent; GridViewRow GVR = (gridviewrow) DCF. Parent; |
"Method Seven"
?
1 2 3 4 5 6 7 8 9 |
<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.
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14-15 16 |
protected void gvinfo_rowcreated (object sender, GridViewRowEventArgs e) {if (E.row.rowtype = = Datacontrolrowtype.datarow) {LinkButton lk1 = (LinkButton) e.row.findcontrol ("lkbtn");//linkbutton's ID 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 is the entire contents of this article, I hope you can enjoy.