简体   繁体   English

如何从C#中的datagridview中删除所选行,以及如何从数据库中删除该记录?

[英]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. 大家好,我想通过在C#Windows窗体的datagridview中选择一行来删除一行,但是我也希望该记录也应该从数据库中删除。我尝试了以下代码,但它不起作用。请帮忙解决此问题问题。

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. 您应该为ITEM_MODEL传递一个值,以将其从行中提取出来。
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" 假设您的网格在名为“ ITEM_MODEL”的列中显示了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. 请注意,我不知道数据库表中ITEM_MODEL列的数据类型。 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. 您正在从Rows集合中删除项目,并且不能通过foreach安全地使用row.Index值。 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. 您的SQL字符串可能是错误的。 I'd assume that you don't want to call row.ToString() for the ITEM_MODEL where row is DataGridViewRow . 我假设您不想为row.ToString()调用row.ToString() ,其中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. 如果要与之比较的ITEM_MODEL位于该行的Tag ,则可以使用row.Tag而不是row

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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