C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Communities   Interview   Jobs   Projects   Offshore Development    
Silverlight Tutorials | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Revenue Sharing |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...

New Feature: Community Sites: Create your own .NET community website and start earning from Google AdSense ! It's Free !






How to delete a row with the click of a button inside gridview in C#


Posted Date: 10 Mar 2008    Resource Type: Code Snippets    Category: ASP.NET GridView

Posted By: idris gani       Member Level: Gold
Rating:     Points: 10



The following code sample shows how to delete a row with the click of a button inside gridview in C#









//code behind
private void dgrdAddressList_DeleteCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
int ContactID = Convert.ToInt32(dgrdAddressList.DataKeys[(int)e.Item.ItemIndex]);
//after getting the id perform delete query
}




Responses

Author: dineshbabu    28 Apr 2008Member Level: Silver   Points : 2
Source:
















SelectCommand="SELECT * FROM [exe1]" DeleteCommand="DELETE FROM [exe1] WHERE [id] = @original_id AND [Name] = @original_Name AND [Salary] = @original_Salary" ConflictDetection="CompareAllValues" InsertCommand="INSERT INTO [exe1] ([id], [Name], [Salary]) VALUES (@id, @Name, @Salary)" OldValuesParameterFormatString="original_{0}" UpdateCommand="UPDATE [exe1] SET [Name] = @Name, [Salary] = @Salary WHERE [id] = @original_id AND [Name] = @original_Name AND [Salary] = @original_Salary">


















 





Codebhind:

protected void btnDelete_Click(object sender, EventArgs e)
{
foreach(GridViewRow row in GridView1.Rows )
{
CheckBox cb=(CheckBox)row.FindControl ("chkgv");
if(cb.Checked)
{
int id = Convert.ToInt32 (GridView1.DataKeys[row.RowIndex].Value);
SqlDataSource1.DeleteParameters["id"].DefaultValue = id.ToString();
SqlDataSource1.Delete();
}
}
}



Author: sanmu    01 May 2008Member Level: Bronze   Points : 2
protected void DeleteRecord(object sender, GridViewDeleteEventArgs e)

{

string autoid = GridView1.DataKeys[e.RowIndex].Value.ToString();



SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnStr"].ToString());

SqlCommand dCmd = new SqlCommand();

try

{

conn.Open();

dCmd.CommandText = "spDeleteData";

dCmd.CommandType = CommandType.StoredProcedure;

dCmd.Parameters.Add("@AutoID", SqlDbType.Int).Value = Int32.Parse(autoid);

dCmd.Connection = conn;

dCmd.ExecuteNonQuery();



lblMessage.Text = "Record Deleted successfully.";



// Refresh the data

BindData();



}

catch (SqlException ee)

{

lblMessage.Text = ee.Message;

}

finally

{

dCmd.Dispose();

conn.Close();

conn.Dispose();

}

}


Feedbacks      
Popular Tags   What are tags ?   Search Tags  
(No tags found.)

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Select / Deselect All records functionality inside GridView.
Previous Resource: Client side validation [Javascript] on click of Datagrid Update button
Return to Discussion Resource Index
Post New Resource
Category: ASP.NET GridView


Post resources and earn money!
 
Related Resources



dotNet Slackers   BizTalk Adaptors    Web Design

silicone halloween mask

Contact Us    Privacy Policy    Terms Of Use