未绑定的gridview上的删除按钮 - C#

时间:2011-07-08 12:20:51

标签: c# .net asp.net linq gridview

有谁能告诉我如何在未绑定的gridview上触发sql delete命令? 是显示LINQ搜索的结果,我有AutoGenerateDeleteButton为true但我不知道如何将其链接到删除查询。

感谢

3 个答案:

答案 0 :(得分:0)

请参阅Insert, Update, and Delete Operations (LINQ to SQL)主题,了解如何使用LINQ实现这些操作。

答案 1 :(得分:0)

每当点击删除按钮时,GridView的RowCommand事件就会触发,你可以在命令名中查看它,如.. e.CommandName == "Delete"

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

答案 2 :(得分:0)

这是我的代码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");
       }

   }