Gets the currently selected row and the index RowCommand

ContractedBlock.gif ExpandedBlockStart.gif Code
acquisition GridView RowCommand current index in line front to add a column template, which add a LinkButton foreground (background value if e.CommandArgument code by the reception code words CommandArgument must set the value in the button, the value of the bound database fields 
< ASP: the TemplateField the HeaderText = " operation " > < the ItemTemplate > < ASP: the LinkButton ID = " LinkButton1 " the runat = " Server "        the CommandName = " QianRu "  the CommandArgument = ' <% # the Eval ( "Id")%> ' > check </ ASP: the LinkButton > <asp:LinkButton ID 
 
  
   
  
= " LinkButton2 "  runat = " Server "  CommandName = " QianChu " > check out </ ASP: LinkButton > </ the ItemTemplate > </ ASP: TemplateField > in the GridView has been set LinkButton event handling button, acquired by the following method index  protected void  gv_Company_RowCommand ( Object  SENDER, GridViewCommandEventArgs E) IF  (e.CommandName  == " QianRu " )   {     . 1 . this is a value obtained by that row is selected index values        
 

 


 DRV the GridViewRow 
=  ((the GridViewRow) (((the LinkButton (e.CommandSource)) parent.parent));. 
2 . This value acquired GridView bound primary key database    
int  ID = Convert.ToInt32 (GridView1, and. the DataKeys [drv.RowIndex] .Value); 
. 3 . this value acquired GridView bound primary key values in the database, the value is the value of the selected row in the first column, drv.RowIndex obtain a selected row index 
int  ID  =  Convert.ToInt32 (GridView1.Rows [drv.RowIndex] .Cells [ 0 ] .Text);  
. 4 . since the client has the primary key and the LinkButton CommandArgument Id to be bound so this ID is the primary key value derived directly e.CommandArgument 
int  ID  =  Convert.ToInt32 (e.CommandArgument.ToString ()); 
// this method does not set in the template column CommandArgument value  String  index
= E.CommandArgument.ToString ();  // that line is selected, the selected row to extract index  int  ID = Convert.ToInt32 (GridView1.Rows [Convert.ToInt32 (index)] Cells [. 0 ] .Text);          }      }  Another is that we do not need to know the current click is the first few lines, you can use the following methods to achieve requirements:    < the ItemTemplate > < ASP: LinkButton ID = " LinkButton1 "  runat = " Server "  CommandArgument = '  <% # Eval ( " field1 ")%> ' the CommandName = " Play "  the Text = '




        
   
 <#% The Eval ( "Field2")%> ' > </ ASP: the LinkButton > </ the ItemTemplate >   above the LinkButton, Text fields bound 2, CommandArgument 1 bound fields    so,   protected void   GridView1_RowCommand ( Object   SENDER, GridViewCommandEventArgs E)    {       IF (e.CommandName = " Play "     {the LinkButton LB  =  (the LinkButton) e.CommandSource;    String   a   =   lb.Text; // here clicked row fields field2 value B = e.CommandArgument String; / / here you can click on to get the value of the row fields field1    
  


        

    }} Or: 
If you are using the template column can be any column bound data to CommandArgument button follows:   
< ASP: the TemplateField > < the ItemTemplate > < ASP: the Button the runat = " Server "  CommandArgument = ' <% # the Eval ( "ID")%> '  the Text = " the button " /> </ the ItemTemplate > </ ASP: the TemplateField > generally bind to a primary key column, so that the primary key may be acquired by the current line in the RowCommand e.CommandArgument, also facilitate other operation    if it is to obtain a row index, a little more trouble, or that Button1, RowDataBound event in the GridView as follows:    the Button btn  =  (the Button) e.Row.FindControl("   
   
    
   
   


Button1 " );   IF  (BTN  =! Null )    {btn.CommandArgument  =  ; e.Row.RowIndex.ToString ()    } This can be by the RowCommand  int  the rowId = Get) Convert.ToInt32 (e.CommandArgument.ToString () the row index   



 

Reproduced in: https: //www.cnblogs.com/hubcarl/archive/2009/03/28/1423927.html

Guess you like

Origin blog.csdn.net/weixin_33753845/article/details/93817203