I used a stupid method. After reading it for two hours, I figured it out.
Rowcommand was used for deletion and update. Later, we found that the value in the Custom input box could not be obtained in rowcommand, so we had to change the update to rowupdating, however, rowupdating does not have the commandargument attribute, so you have to put a hiddenfield for Parameter Storage. It's helpless. I don't know how to use it. Can you give me some advice?
How to operate data in the gridview:
1. Delete:
Create a button in the template and set its commandtype = "Del", commandargument = "<% # eval (" ID ") %>"
Determine commandtype in the rowcommand event of the gridview and delete data with commandargument as the parameter
2. Update:
Create a button in the template and set its commandtype = "Update"
Create a hiddenfield and set it to value = "<% # eval (" ID ") %>"
In the rowupdating event of the gridview, first use E. rowindex to find the index of the current row, and then use findcontrol to get the value and update it.
3. Update 2:
Another method is to write <% # (gridviewrow). rowindex %> In commandargument of the "edit" button.
In the rowcommand event of the gridview, you can use commandargument to obtain the index of the current row and then process it.
BelowCodeIs not implemented using this method
Protected Void Gridview1_rowcommand ( Object Sender, gridviewcommandeventargs E)
{
If (E. commandname = " Del " )
{
Int Index = Convert. toint32 (E. commandargument );
String Sqlcmd = " Delete from Admin where id = " + Index;
Usersql. DB. executenonquery (sqlcmd );
Databinded ();
}
}
Protected Void Gridview1_rowupdating ( Object Sender, gridviewupdateeventargs E)
{
Int Index2 = E. rowindex;
String Txtname = (Textbox) This . Gridview1.rows [index2]. cells [ 2 ]. Findcontrol ( " Txtadminname " ). Text;
String Txtpass = (Textbox) This . Gridview1.rows [index2]. cells [ 2 ]. Findcontrol ( " Txtadminpassword " ). Text;
Int ID = Convert. toint32 (hiddenfield) This . Gridview1.rows [index2]. cells [ 2 ]. Findcontrol ( " Hiddenid " ). Value );
String Sqlcmd = " Update [admin] Set [adminname] = @ adminname, [adminpassword] = @ adminpassowrd where [ID] = @ ID " ;
Sqlparameter [] Param = {
New Sqlparameter ( " @ Adminname " , Txtname ),
New Sqlparameter ( " @ Adminpassowrd " , Txtpass ),
New Sqlparameter ( " @ ID " , ID ),
} ;
Usersql. DB. executenonquery (sqlcmd, Param );
Databinded ();
}