First we drag a DataGridView control into the. aspx page, and then bind the columns you want to display, as shown in the following code.
Copy Code code as follows:
<asp:gridview id= "gvdepartlist" runat= "Server" autogeneratecolumns= "False"
height= "108px" width= "600px" onrowdeleting= "gvdepartlist_rowdeleting" rowdatabound= "Gvdepartlist_rowdataround" >
<Columns>
<asp:templatefield headertext= "department name" >
<ItemTemplate>
<asp:label runat= "Server" style= "Text-align:center" text= ' <%# Eval ("Departname")%> '/>
</ItemTemplate>
</asp:TemplateField>
<asp:boundfield headertext= "agency" datafield= "Branchid"/>
<asp:boundfield headertext= "responsible" datafield= "Principaluser"/>
<asp:boundfield headertext= "Contact phone" datafield= "Connecttelno"/>
<asp:boundfield headertext= "mobile phone" datafield= "Connectmobiletelno"/>
<asp:boundfield headertext= "Fax" datafield= "Faxes"/>
<asp:templatefield headertext= "Modify" >
<ItemTemplate>
<asp:imagebutton id= "ImageButton1" imageurl= ". /images/edit.gif "Commandargument= ' <% #Eval (" Departid ")%> ' commandname= ' delete ' runat= ' server '/>
</ItemTemplate>
</asp:TemplateField>
<asp:templatefield headertext= "Delete" >
<ItemTemplate>
<asp:imagebutton imageurl= ". /images/delete.gif "Commandargument= ' <% #Eval (" Departid ")%> ' commandname= ' delete ' runat= ' server '/>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Two: Bind the data in the Page_Load event in the background of this. aspx page.
Copy Code code as follows:
protected void Page_Load (object sender, EventArgs e)
{
if (! IsPostBack)
{
Gvdepartlist.datasource= new Departinfomanager (). Getdepartinfos (-1);
Gvdepartlist.databind ();
}
}
If we want to add a DataGridView effect, that is, each row of mouse hover up to change the background color.
Copy Code code as follows:
<summary>
Dynamic registration Script (before the GridView control renders) light stick effect
</summary>
<param name= "Sender" ></param>
<param name= "E" ></param>
protected void Gvusers_rowdatabound (object sender, GridViewRowEventArgs e)
{
Here the only data row in the script registration
if (E.row.rowtype = = Datacontrolrowtype.datarow)
{
Light Bar Effect
E.row.attributes.add ("onmouseover", "currentcolor=this.style.backgroundcolor;this.style.backgroundcolor=") 6699ff ' ");
E.row.attributes.add ("onmouseout", "This.style.backgroundcolor=currentcolor");
LinkButton Lnkbtndel = E.row.findcontrol ("Lnkbtndel") as LinkButton;
LNKBTNDEL.ATTRIBUTES.ADD ("onclick", "return confirm (' OK delete? ')");
}
}
Now the point is, how is the line of data? Since it's a deletion, we're definitely going to delete it based on the ID of a piece of data, so we add a code to the Page_Load method:
Gvdepartlist.datakeynames = new string[] {"id"};//What this code means is that each line sets a key that is used to manipulate the data.
Now we use another method to delete, see the penultimate column in the page, yes, is a Imagebuttom control, this control is placed a Delete button small icon, CommandArgument is what? What's commandname doing? CommandArgument is to specify the parameters we want to manipulate, CommandName is the command what is the button for? Here is the deletion, and we write the delete.
Copy Code code as follows:
<asp:templatefield headertext= "Delete" >
<ItemTemplate>
<asp:imagebutton imageurl= ". /images/delete.gif "Commandargument= ' <% #Eval (" Departid ")%> ' commandname= ' delete ' runat= ' server '/>
</ItemTemplate>
</asp:TemplateField>
Next is the background operation code, you can see this DataGridView binding a onrowdeleting event, this event is used to delete.
Then we write this code in this event.
Copy Code code as follows:
<summary>
Delete the selected row
</summary>
<param name= "Sender" ></param>
<param name= "E" ></param>
protected void Gvdepartlist_rowdeleting (object sender, Gridviewdeleteeventargs e)
{
ImageButton buttom = Gvdepartlist.rows[e.rowindex]. FindControl ("Btndelete") as ImageButton;
String departid = Buttom.CommandArgument.ToString ();
if (manage. Deletedepart (Departid))
{
Page.ClientScript.RegisterClientScriptBlock (this. GetType (), "alert", "<script>alert (' delete successful! '); </script> ");
Binddepartinfos ()//rebind data
}
Else
{
Page.ClientScript.RegisterClientScriptBlock (this. GetType (), "alert", "<script>alert (' delete failed! '); </script> ");
}
}
For a better user experience, we can not use the Page.ClientScript.RegisterClientScriptBlock (this. GetType (), "alert", "<script>alert (' delete successful! '); </script> ");
You can choose to place a label control prominently in the page, design visible=false, hide it, and then delete the successful, use this label control to prompt the user, delete success!