我需要根据网格视图中选定的值从数据表中删除一行
我试图成功,但我失败了
protected void GV_Repair_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
var row = GV_Repair.Rows[e.RowIndex];
newdt.Rows.Remove();
bind_grid();
}
答案 0 :(得分:0)
如果您有静态数据源,则可以执行以下操作:
protected void GV_Repair_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
newdt.Rows.RemoveAt(e.RowIndex);
bind_grid();
}
如果数据源是动态的,则在GridView的模板字段中需要一个隐藏字段(如果您还没有):
<asp:TemplateField>
<ItemTemplate>
<asp:HiddenField ID="hdnId" runat="server" Value='<%# Eval("ID") %>' />
<!-- Other Items -->
</ItemTemplate>
</asp:TemplateField>
并更改行删除方法:
protected void GV_Repair_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
int id = 0;
GridViewRow row = GV_Repair.Rows[e.RowIndex];
HiddenField hdnId = row.FindControls("hdnId") as HiddenField;
if(hdnId != null && int.TryParse(hdnId.Value, out id)
{
//Logic to delete from table where Id = id
}
bind_grid();
}