简体   繁体   中英

How to delete selected row from datagridview in C# and also delete that record from database?

Hi everyone, I want to delete a row by selecting a row in datagridview in C# windows form ,but i also want that this record should also be deleted from database .I have tried following code but its not working.Kindly help out to solve this issue.

Code:

  using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["CS"].ConnectionString))
            {
                SqlCommand delcmd = new SqlCommand();

                foreach (DataGridViewRow row in dg.Rows)
                {
                    if (row.Selected)
                    {
                        dg.Rows.RemoveAt(row.Index);
                        try
                        {
                            delcmd.CommandText = "Delete From ITEM_DETAILS Where ITEM_MODEL=" + row + "";
                            con.Open();
                            delcmd.Connection = con;
                            delcmd.ExecuteNonQuery();
                        }
                        catch(Exception ex)
                        {
                            MessageBox.Show(ex.ToString());
                        }
                    }

                }
            }
        }

You should pass a value for ITEM_MODEL extracting it from your row.
You cannot use the whole row for that. And in any case you should use a parameterized query.

Supposing that the value for ITEM_MODEL is displayed by your grid in a column named "ITEM_MODEL"

try
{
    delcmd.CommandText = "Delete From ITEM_DETAILS Where ITEM_MODEL=@item";
    con.Open();
    delcmd.Connection = con;
    delcmd.Parameters.AddWithValue("@item", row.Cells["ITEM_MODEL"].Value.ToString());
    delcmd.ExecuteNonQuery();
}

Notice that I don't know the datatype of the column ITEM_MODEL in your database table. I assume that it is a string so the creation of a parameter of string type. If this assumption is not true then the parameter should be created using an appropriate conversion from the value stored in the grid column

I would also suggest to change your loop over the Grid rows. You are removing items from the Rows collection and you cannot use the row.Index value in a safe way using a foreach. Better use a traditional loop moving backward from the end to the first row

for(int x = dg.Rows.Count - 1; x >= 0; x--)
{
    DataGridRow row = dg.Rows[x];
    if (row.Selected)
    {
        dg.Rows.RemoveAt(row.Index);
        .....

Your SQL string is probably wrong. I'd assume that you don't want to call row.ToString() for the ITEM_MODEL where row is DataGridViewRow .
If the ITEM_MODEL that you want to compare it to is in the Tag of the row, you could use row.Tag instead of row , for example.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM