嗨大家好, 我想通过在C#窗体中的datagridview中选择一行来删除一行,但我也希望这个记录也应该从数据库中删除。我已经尝试了下面的代码,但它不工作。善良的帮助解决这个问题。如何从C#中的datagridview中删除选定的行,并从数据库中删除该记录?
代码:
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());
}
}
}
}
}
不回答你的问题,但你最好在你的commandtext中使用参数。 – Sybren 2014-11-02 21:01:41