How to delete / edit a row in gridview dynamically with buttons present in gridview itself

Posted by Kumartyr under ASP.NET on 11/12/2013 | Points: 10 | Views : 2495 | Status : [Member] | Replies : 3
How to delete / edit a row in gridview dynamically with buttons present in grid itself

i have tried rowdeleting event for deleting & rowediting for edit but no use

using VS2005 asp.net C# 2.0

below is what i tried in rowdeleting event

protected void GridViewFRM_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
if (connection.State == ConnectionState.Closed)
{
connection.Open();
}
OracleCommand OLCOM3 = new OracleCommand();
OLCOM3.CommandText = "Delete from FORUM WHERE QUESTION='" + GridViewFRM.DataKeys[e.RowIndex].Values[0].ToString() + "'";
OLCOM3.ExecuteNonQuery();
DisplayUserData();
}




Responses

Posted by: Bandi on: 11/12/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
http://www.nullskull.com/a/1645/aspnet-basics-gridview-from-database-with-paging-sorting-and-editing.aspx

Refer
http://aspdotnetbank-kartik.blogspot.in/2012/01/bind-gridview-using-datatable-with.html
http://www.dotnetspider.com/resources/20462-Bind-Gridview-from-Session-Then-Edit-Update.aspx
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowdeleting(v=vs.110).aspx

Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Kumartyr, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bandi on: 11/12/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
Sample code:
protected void gvEmployee_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ToString()))
{
using (SqlCommand cmd = new SqlCommand())
{
// Create a command object.


// Assign the connection to the command.
cmd.Connection = conn;

// Set the command text
// SQL statement or the name of the stored procedure
cmd.CommandText = "DELETE FROM Employees WHERE employeeID = @EmployeeID";

// Set the command type
// CommandType.Text for ordinary SQL statements;
// CommandType.StoredProcedure for stored procedures.
cmd.CommandType = CommandType.Text;

// Get the EmployeeID of the selected row.
string strEmployeeID = gvEmployee.Rows[e.RowIndex].Cells[2].Text;

// Append the parameter.
cmd.Parameters.Add("@EmployeeID", SqlDbType.Int).Value = strEmployeeID;

// Open the connection.
conn.Open();

// Execute the command.
cmd.ExecuteNonQuery();
}
}

// Rebind the GridView control to show data after deleting.
BindGridView();
}



Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Kumartyr, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: vishalneeraj-24503 on: 11/12/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
http://www.encodedna.com/2013/01/asp.net-gridview-edit-delete.htm

Kumartyr, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response