Hi I have used gridview to create a table. Is there a way to implement edit and delete. I have done it in PHP before. The method I would like to use is create two more columns in the table with edit and delete buttons on each row. Then when the buttons are click it passes the 'id' through the URL and able to edit or delete. Not really sure how to do this in asp.net webforms. Below is my code for the table. Thank you.
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:BoundField HeaderText="Surgery" DataField="surgery" />
<asp:BoundField HeaderText="PatientID" DataField="patientID" />
<asp:BoundField HeaderText="Location" DataField="location" />
</Columns>
SqlCommand cmd = new SqlCommand("select surgery, patientID, location from details", conn);
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
sda.Fill(dt);
conn.Close();
GridView1.DataSource = dt;
GridView1.DataBind();
The GridView supports those operations. You can add a
CommandField
which will contain the command buttons or LinkButtons (you can choose the type of button and assign the text of each button). ThepatientID
field should be included in theDataKeyNames
property of the GridView, in order to retrieve it when the time comes to update or delete the record in the database.You will then need to handle a few events in code-behind:
Since the data must be bound to the GridView at the end of each of those event handlers, you can do it in a
BindData
utility function, which should also be called when the page loads initially: