DataGrid 按钮列 删除功能怎么实现 就是里面的按纽列里有个 删除中 然后怎么实现删除该行的功能!!! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 private void DataGrid1_DeleteCommand(object sender, DataGridCommandEventArgs e) { if(e.CommandName=="del") { db=new DBConnection(); SqlConnection conn=db.OpenConnection(); string s=e.Item.Cells[1].Text; Sql = "DELETE FROM newstable WHERE id = " + s; SqlCommand sqlCommandDel = new SqlCommand(Sql,conn); try { sqlCommandDel.ExecuteNonQuery(); } catch(Exception ex) { Response.Write(ex.ToString()); } finally { db.CloseConnection(); } } } del 那一列的标示 如果你是删除按钮的话直接在的deletecommand事件下写代码,如果是选择按钮改写的话,在item_command事件下写代码就行了 看你这个按钮的CommandName了,如果是delete,可以在DeleteCommand事件中处理,否则可以在ItemCommand中处理 得到标示应该是if(e.CommandName=="del"){//得到选中行的主键 int id=this.DataGrid1.Items[(int)e.Item.ItemIndex];//你要是多行选择删除的话 那你就用foreach做一下遍历吧}//或是通过按钮上面的字做操作if(((Button)e.CommandSource).Text=="删除"){ } // .aspx<asp:DataGrid ID="DataGrid1" DataKeyField="ID" runat="server" OnDeleteCommand="DataGrid1_DeleteCommand"> <Columns> <asp:TemplateColumn> <ItemTemplate> <asp:LinkButton ID="btnDelete" runat="server" CausesValidation="false" CommandName="Delete" Text="Delete" OnClientClick="if(!confirm('deleting?')) return false;"></asp:LinkButton> </ItemTemplate> </asp:TemplateColumn> </Columns> </asp:DataGrid>// .aspx.cs protected void DataGrid1_DeleteCommand(object source, DataGridCommandEventArgs e) { // 获取 ID object id = DataGrid1.DataKeys[e.Item.ItemIndex]; // 执行删除 // ... // ... // 重新绑定数据 // ... } 汗 上面DataGrid1.Items[(int)e.Item.ItemIndex];写错了 不好意思 自定义错误页面 and 附近有语法错误,找了半天不知道是哪的问题 用户控件的问题 aspx站点访问效率,gridview中控件的执行效率的疑问 关于空格与回车是如何存进数据库的 非常简单的广告管理系统? 跪求《ASP.NET揭密》源代码 关于强制类型转换的效率问题 @ASP.NET:找不到aspnet_CheckSchemaVersion 100!!!!!updateCommand事件写更新的? 在grid的column上回车动作后,光标自动跳转到另一个命令按纽上? 20分问一简单问题
{
if(e.CommandName=="del")
{
db=new DBConnection();
SqlConnection conn=db.OpenConnection();
string s=e.Item.Cells[1].Text;
Sql = "DELETE FROM newstable WHERE id = " + s;
SqlCommand sqlCommandDel = new SqlCommand(Sql,conn);
try
{
sqlCommandDel.ExecuteNonQuery();
}
catch(Exception ex)
{
Response.Write(ex.ToString());
}
finally
{
db.CloseConnection();
}
}
}
del 那一列的标示
if(e.CommandName=="del")
{
//得到选中行的主键
int id=this.DataGrid1.Items[(int)e.Item.ItemIndex];
//你要是多行选择删除的话 那你就用foreach做一下遍历吧
}//或是通过按钮上面的字做操作
if(((Button)e.CommandSource).Text=="删除")
{
}
<asp:DataGrid ID="DataGrid1" DataKeyField="ID" runat="server" OnDeleteCommand="DataGrid1_DeleteCommand">
<Columns>
<asp:TemplateColumn>
<ItemTemplate>
<asp:LinkButton ID="btnDelete" runat="server" CausesValidation="false" CommandName="Delete" Text="Delete" OnClientClick="if(!confirm('deleting?')) return false;"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>// .aspx.cs
protected void DataGrid1_DeleteCommand(object source, DataGridCommandEventArgs e)
{
// 获取 ID
object id = DataGrid1.DataKeys[e.Item.ItemIndex];
// 执行删除
// ...
// ...
// 重新绑定数据
// ...
}