我想在C#Windows窗體從DataGridView刪除行,我的代碼只是工作的一次再刪除排在C#Windows窗體
if (dataGridView1.Columns[e.ColumnIndex].Name == "Delete")
{
try
{
using (con = new SqlConnection(C))
{
using (objCom = new SqlCommand("DELETE FROM Crud where Id = @Id;SELECT id as 'Customer Id' , Ch_Code as 'Customer Code' , Ch_Name as 'Customer Name',Ch_Type as 'Type' FROM Crud", con))
{
objCom.Parameters.AddWithValue("@id", dataGridView1.Rows[e.RowIndex].Cells[0].Value);
using (objAdp = new SqlDataAdapter(objCom))
{
objAdp.Fill(DS,"xyz");
dataGridView1.DataSource = DS.Tables["xyz"].DefaultView;
}
}
}
}
catch (Exception EX)
{
MessageBox.Show(EX.Message);
}
}
這讓我異常
轉換失敗時轉換爲nvarchar瓦魯爾足球俱樂部「刪除」到數據 int類型
你的'dataGridView1.Rows [e.RowIndex] .Cells [0] .Value'似乎有'Delete'作爲'object'。你如何看待你可以刪除這個只有「ID」列的整數值的值? –