删除未绑定gridview上的按钮

本文关键字:按钮 gridview 绑定 删除 | 更新日期: 2023-09-27 18:01:28

谁能告诉我如何在未绑定的gridview上触发sql删除命令?它显示了LINQ搜索的结果,我有AutoGenerateDeleteButton为真,但我不确定如何将此链接到删除查询。

谢谢

删除未绑定gridview上的按钮

每当点击Delete按钮时,GridView的RowCommand Event就会触发,在那里你可以通过Command Name检查它,比如…e.CommandName == "Delete"

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
    if (e.CommandName == "Delete")
    {
        // Put your Deletion code here.....     
    }
}

请参阅插入、更新和删除操作(LINQ to SQL)主题,该主题展示了如何使用LINQ实现这些操作。

这是我的代码Theresa,希望能有所帮助。

private void deleteButton_Click(object sender, RoutedEventArgs e)
   {
       try
       {
           DBConnDataContext db = new DBConnDataContext();
           tbWellClassification shortName = TableGrid.SelectedItem as tbWellClassification;
           var well = (from s in db.tbWellClassifications
                       where s.shortName == shortName.shortName
                       select s).Single();
           db.tbWellClassifications.DeleteOnSubmit(well);
           db.SubmitChanges();
           MessageBox.Show("Row Deleted Successfully.");
           txtStatus.Text = "Row Deleted";
           db = null;
           DBConnDataContext db2 = new DBConnDataContext();
           TableGrid.ItemsSource = db2.tbWellClassifications;
           TableGrid.Items.Refresh();
       }
       catch
       {
           MessageBox.Show("Delete Unsuccessful");
       }
   }