ASP.NET uses gridview to get the index value of the current row

  • 2021-06-28 12:15:09
  • OfStack

When using the GridView control, we often encounter an index to get the current row and do a lot of things with it.For example, you can get a control element for the current row;Set the value of an element 1, etc.Below are some examples of how to obtain the current row index value for GridView.

Example:
Objective: To obtain the current index row of RowCommand in GridView.
(2) Front page: Add a template column in GridView, and add an LinkButton control inside.
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"> have sth. signed  </asp:LinkButton> 
</ItemTemplate> 
</asp:TemplateField> 

Tip: If you use e.CommandArgument in the background code, the foreground code must set the value of CommandArgument in the button, which is a bound database field.For example:
//Because CommandArgument of LinkButton is already bound to primary key Id in the client, the value of primary key ID can be derived directly from e.CommandArgument here.
int id = Convert.ToInt32(e.CommandArgument.ToString());
(3) LinkButton has been set as the event handling button in GridView, and the index will be obtained by the following methods:


protected void gv_Company_RowCommand(object sender, GridViewCommandEventArgs e){ 
if (e.CommandName == "QianRu") 

{ 

[Method 1]


GridViewRow drv = ((GridViewRow)(((LinkButton)(e.CommandSource)).Parent.Parent)); // The resulting value is the index value indicating that the row is selected  
inf id=Convert.ToInt32(GridView1.DataKeys[drv.RowIndex].Value); // The value obtained is GridView Bind primary key values in database in  

Note: Using this method, the DataKeyNames property of GridView needs to be set, in this case as the primary key field.

[Method 2]


GridViewRow drv = (GridViewRow)((LinkButton)e.CommandSource).NamingContainer;// The resulting value is the index value indicating that the row is selected  
int id = Convert.ToInt32(GridView1.Rows[drv.RowIndex].Cells[0].Text); // The value obtained is GridView Bind primary key values in database in , Value method is the first of the selected rows 1 Value of column ,drv.RowIndex Gets the index of the selected row  
} 
} 

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

[Method 3]

In the Command event of the linkbutton control, use Parent of sender to get the current row in 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 indicating that the row is selected  
lbtnQianChu.SelectedIndex = gvr.RowIndex; 
} 

[Method 4]

In the Click event of the linkbutton control, get the current row in GridView.


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

[Method 5]

If you add an DropDownList control under 1 to the template column and turn on its AutoPostback property, in the SelectedIndexChanged event of DropDownList, get the current row in GridView.

Here 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 uses the control's NamingContainer property to get its container, the GridViewRow object.
Tip: Because DropDoweList is different from button, its CommandName cannot be specified, so use the NamingContainer attribute to solve the problem.
Let's start with Microsoft's explanation of the NamingContainer attribute:
Gets a reference to a named container of a server control that creates a 1-only namespace to distinguish server controls with the same Control.ID property value.
Each page of the ASP.NET Web application contains a hierarchy of controls.This hierarchy is independent of whether the control generates a user-visible UI.The named container for a given control is the parent control above it in the hierarchy that implements the INamingContainer interface.The server control that implements this interface creates a 1-only namespace for the ID property value of its child server control.

Creating a 1-only namespace for a server control is particularly 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 that is a child of a duplicate control, the naming container ensures that each instance of these child controls has a non-conflicting UniqueID property value.The default naming container for a page is an instance of the Page class that is generated when the page is requested.
You can use this property to determine the named container in which a particular server control resides.

[Method 6]

If there is an CheckBox control in the template column, pass through CheckBox1_In the CheckedChanged event, get the current row in GridView.


CheckBox chk = (CheckBox)sender; 
DataControlFieldCell dcf = (DataControlFieldCell)chk.Parent; 
GridViewRow gvr = (GridViewRow)dcf.Parent; 

[Method 7]


<asp:GridView ID="gvTest" runat="server"> 
<Columns> 
<asp:TemplateField> 
<ItemTemplate> 
DisplayIndex : <%# Container.DisplayIndex %> || DataItemIndex : <%# Container.DataItemIndex %><br /> 
</ItemTemplate> 
</asp:TemplateField> 
</Columns> 
</asp:GridView> 

[Method 8]

The control's ID and Name naming can be done as above. I need to use the RowCommand() method to determine which column is selected. To use this method, I need to know an attribute like e.CommandArgument (first I have to know that in GridView, the row index is placed in CommandArgument), and now the task is to get such an attribute.Looking at the data, you know that each row in the GridView control is created with an RowCreated event, which allows you to write the line number selected by linkButton to CommandArgument.


protected void gvInfo_RowCreated(object sender, GridViewRowEventArgs e) 
{ 
if (e.Row.RowType == DataControlRowType.DataRow) 
{ 
LinkButton lk1 = (LinkButton)e.Row.FindControl("lkbtn");//LinkButton Of ID 
lk1.CommandArgument = e.Row.RowIndex.ToString(); 
} 
} 
protected void gvInfo_RowCommand(object sender, GridViewCommandEventArgs e) 
{ 
if (e.CommandName == "ADD")// I LinkButton Of CommandName 
{ 
int index = Convert.ToInt32(e.CommandArgument); 
string aa = gvInfo.Rows[index].Cells[1].Text.ToString();// Gets the current row and column number as 1 Value of, column number from 0 start  
} 
} 

The above is the whole content of this article, I hope you like it.


Related articles: