【发布时间】:2011-07-08 12:20:51
【问题描述】:
谁能告诉我如何在未绑定的 gridview 上触发 sql delete 命令? Is 显示 LINQ 搜索的结果,我将 AutoGenerateDeleteButton 设置为 true,但我不确定如何将其链接到删除查询。
谢谢
【问题讨论】:
标签: c# .net asp.net linq gridview
谁能告诉我如何在未绑定的 gridview 上触发 sql delete 命令? Is 显示 LINQ 搜索的结果,我将 AutoGenerateDeleteButton 设置为 true,但我不确定如何将其链接到删除查询。
谢谢
【问题讨论】:
标签: c# .net asp.net linq gridview
每当点击 Delete 按钮时,GridView 的 RowCommand 事件就会触发,您可以在此处通过命令名称检查它,例如..e.CommandName == "Delete"
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Delete")
{
// Put your Deletion code here.....
}
}
【讨论】:
请参阅Insert, Update, and Delete Operations (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");
}
}
【讨论】: