Click here to Skip to main content
15,889,403 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to delete a selected row in the gridview, the User has a option of selecting the row to be deleted and he can delete only one row at a time. How can this be done.

Under the .CS page i have written the following code.
C#
protected void gdvData_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            InBoundDataBO obj = new InBoundDataBO();
            int SUPPLIER_NUMBER = Convert.ToInt32(gdvData.Rows[e.RowIndex].Cells[4].Text);
            string SOURCE_ENTITY = gdvData.Rows[e.RowIndex].Cells[2].Text.ToString();
            string PLANT_CODE = gdvData.Rows[e.RowIndex].Cells[3].Text.ToString();
            string SUPPLIER_GROUP_NUMBER = gdvData.Rows[e.RowIndex].Cells[5].Text.ToString();

            obj.DeleteSupplierDetails(SUPPLIER_NUMBER, SOURCE_ENTITY, PLANT_CODE, SUPPLIER_GROUP_NUMBER);
        }


Under the Data Acess Layer i have written the following method.

C#
public static void DeleteSupplierDetails(String R1, String R2, String R3, String R4,out String message)
                {  
                    message = String.Empty;
                    Database db =   Helper.CreateDatabase();

                    DbCommand cmd = db.GetStoredProcCommand("PDSS_STAGING_DEV.Delete_SupplierDetails");
                    cmd.CommandType = CommandType.StoredProcedure;


                    db.AddInParameter(cmd, "P_SOURCE_ENTITY", DbType.String, R1);
                    db.AddInParameter(cmd, "P_PLANT_CODE", DbType.String, R2);
                    db.AddInParameter(cmd, "P_SUPPLIER_NUMBER", DbType.String, R3);
                    db.AddInParameter(cmd, "P_SUPPLIER_GROUP_NUMBER", DbType.String, R4);
                    db.AddOutParameter(cmd,"P_RETURN_MSG", DbType.String,10);
                    db.ExecuteNonQuery(cmd);
                    message = cmd.Parameters["P_RETURN_MSG"].Value.ToString().Trim();
                }
Posted
Updated 12-Aug-13 21:46pm
v4
Comments
So what is the problem? Where are you stuck?
Rock_Dead 13-Aug-13 4:34am    
The following code is not working and i am not able to delete the selected item
Please debug and check each step. And what have you inside the procedure PDSS_STAGING_DEV.Delete_SupplierDetails?

Is your application throwing any error/exception?
Shahan Ayyub 13-Aug-13 5:20am    
Is this Windows Forms ? or Web Form ?
Rock_Dead 13-Aug-13 6:22am    
Web App

1 solution

This code removes selected items of dataGridView1:
C#
private void btnDelete_Click(object sender, EventArgs e)
{
    foreach (DataGridViewRow item in this.dataGridView1.SelectedRows)
    {
        dataGridView1.Rows.RemoveAt(item.Index);
    }
}

Also have a look at-
CodeProject[^]
Removing-selected-rows-in-a-datagridview-in-c[^]
How-to-remove-selected-row-in-data-grid-view-C-Sharp.aspx[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900