如何在datagrid中实现删除功能(在线等待,问题解决立即结贴) DataGrid本身就有DeleteCommand事件。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 我知道但是不知道怎么用,能给个示例代码么,aspx和cs的都要。 cs的在ItemDataBound事件里 SqlConnection myConn = new SqlConnection(ConfigurationSettings.AppSettings["connStr"]); myConn.Open() ; string SQL="delete from tblName where iId="+id; SqlCommand myCommand = new SqlCommand(SQL,myConn); myCommand.ExecuteNonQuery(); myConn.Close() ; datagrid添加删除列,设置DataKeyField="主键字段"在DeleteCommand事件中e是 DataGridCommandEventArgs estring id = datagrid.DataKeys[e.Item.ItemIndex].ToString();string aSql = "delete from table where id=" + id;//调用Common对象的方法comm.ExecuteNonQuery() Apollo2003(Apollo2003) 说的很详细了。 http://chs.gotdotnet.com/quickstart/aspplus/doc/webdataaccess.aspx#delete asp:<asp:DataGrid id="DataGrid1" runat="server" DataKeyField="字段"></asp:DataGrid>cs: private void DataGrid1_DeleteCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e) { string id = DataGrid1.DataKeys[e.Item.ItemIndex].ToString(); //delete方法 } <asp:DataGrid id="DataGrid1" runat="server" DataKeyField="字段"> <Columns> <asp:ButtonColumn Text="delete" CommandName="delete"></asp:ButtonColumn> </Columns> </asp:DataGrid> 我试了还是不行,他就不执行delete的那个事件,我在里边放了个label他根本就显示不出来 <ASP:DataGrid id="MyDataGrid" runat="server" DataKeyField="id" OnDeleteCommand="MyDataGrid_Delete" >public void MyDataGrid_Delete(Object sender, DataGridCommandEventArgs e) { String deleteCmd = "DELETE from Employee where id = @Id"; SqlCommand myCommand = new SqlCommand(deleteCmd, myConnection); myCommand.Parameters.Add(new SqlParameter("@Id", SqlDbType.NVarChar, 11)); myCommand.Parameters["@Id"].Value = MyDataGrid.DataKeys[(int)e.Item.ItemIndex]; myCommand.Connection.Open(); try { myCommand.ExecuteNonQuery(); Message.InnerHtml = "<b>已删除记录</b><br>" + deleteCmd; } catch (SqlException) { Message.InnerHtml = "错误:未能删除记录"; Message.Style["颜色"] = "红色"; } myCommand.Connection.Close(); BindGrid(); } 另外检查一下事件注册在不在private void InitializeComponent() { this.MyDataGrid.DeleteCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.MyDataGrid_Delete); } FROM 子句语法错误。 说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.Data.OleDb.OleDbException: FROM 子句语法错误。源错误: 行 63: string aSql = "delete from table where ID="+id;行 64: OleDbCommand comm=new OleDbCommand(aSql,mycon);行 65: comm.ExecuteNonQuery();行 66: grid.DataBind();行 67: mycon.Close(); FROM 子句语法错误.看看id值是多少?字段里有没有ID字段?表名对了没有~? 我的cspublic void DeleteCommand(object sender, DataGridCommandEventArgs e) { string constr; constr="Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+Server.MapPath("db/db1.mdb"); OleDbConnection mycon=new OleDbConnection(constr); mycon.Open(); string id = grid.DataKeys[e.Item.ItemIndex].ToString(); string aSql = "delete from table where ID="+id; OleDbCommand comm=new OleDbCommand(aSql,mycon); comm.ExecuteNonQuery(); grid.DataBind(); mycon.Close(); prompt.Text="888"; } aspx<asp:datagrid id="grid" DataKeyField="ID" runat="server" OnDeleteCommand="DeleteCommand" OnPageIndexChanged="changegrid" AllowPaging="True" Width="600px" CellPadding="3" BorderWidth="1px" BorderColor="Black" PageSize="5" AutoGenerateColumns="False" HeaderStyle-BackColor="#aaaadd" Font-Size="10pt" HorizontalAlign="Left" Font-Names="宋体"> 希望各位给一个建议 关于ASP.NET的和sql server 2005配置环境 如何在datagrid中调用试图 数据库连接在服务器调试时需要改动sqlConnection吗? 请问有人知道关于<% 值 %>的问题吗? 在html页中. 怎样禁止用户直接输入url来访问页面? 求一正则表达式 vs2005中DropDownList1_SelectedIndexChanged不执行! 正则表达式问题 二进制图片显示问题 如何格式化一个SQL 语句的结果? beta2问题
SqlConnection myConn = new SqlConnection(ConfigurationSettings.AppSettings["connStr"]);
myConn.Open() ;
string SQL="delete from tblName where iId="+id;
SqlCommand myCommand = new SqlCommand(SQL,myConn);
myCommand.ExecuteNonQuery();
myConn.Close() ;
在DeleteCommand事件中
e是 DataGridCommandEventArgs e
string id = datagrid.DataKeys[e.Item.ItemIndex].ToString();
string aSql = "delete from table where id=" + id;
//调用Common对象的方法
comm.ExecuteNonQuery()
<asp:DataGrid id="DataGrid1" runat="server" DataKeyField="字段"></asp:DataGrid>
cs:
private void DataGrid1_DeleteCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
string id = DataGrid1.DataKeys[e.Item.ItemIndex].ToString();
//delete方法
}
<Columns>
<asp:ButtonColumn Text="delete" CommandName="delete"></asp:ButtonColumn>
</Columns>
</asp:DataGrid>
DataKeyField="id"
OnDeleteCommand="MyDataGrid_Delete"
>
public void MyDataGrid_Delete(Object sender, DataGridCommandEventArgs e)
{
String deleteCmd = "DELETE from Employee where id = @Id"; SqlCommand myCommand = new SqlCommand(deleteCmd, myConnection);
myCommand.Parameters.Add(new SqlParameter("@Id", SqlDbType.NVarChar, 11));
myCommand.Parameters["@Id"].Value = MyDataGrid.DataKeys[(int)e.Item.ItemIndex]; myCommand.Connection.Open(); try
{
myCommand.ExecuteNonQuery();
Message.InnerHtml = "<b>已删除记录</b><br>" + deleteCmd;
}
catch (SqlException)
{
Message.InnerHtml = "错误:未能删除记录";
Message.Style["颜色"] = "红色";
} myCommand.Connection.Close(); BindGrid();
}
private void InitializeComponent()
{
this.MyDataGrid.DeleteCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.MyDataGrid_Delete);
}
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.Data.OleDb.OleDbException: FROM 子句语法错误。源错误:
行 63: string aSql = "delete from table where ID="+id;
行 64: OleDbCommand comm=new OleDbCommand(aSql,mycon);
行 65: comm.ExecuteNonQuery();
行 66: grid.DataBind();
行 67: mycon.Close();
public void DeleteCommand(object sender, DataGridCommandEventArgs e)
{
string constr;
constr="Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+Server.MapPath("db/db1.mdb");
OleDbConnection mycon=new OleDbConnection(constr);
mycon.Open();
string id = grid.DataKeys[e.Item.ItemIndex].ToString();
string aSql = "delete from table where ID="+id;
OleDbCommand comm=new OleDbCommand(aSql,mycon);
comm.ExecuteNonQuery();
grid.DataBind();
mycon.Close();
prompt.Text="888";
}
<asp:datagrid id="grid" DataKeyField="ID" runat="server" OnDeleteCommand="DeleteCommand" OnPageIndexChanged="changegrid" AllowPaging="True" Width="600px" CellPadding="3" BorderWidth="1px" BorderColor="Black" PageSize="5" AutoGenerateColumns="False" HeaderStyle-BackColor="#aaaadd" Font-Size="10pt" HorizontalAlign="Left" Font-Names="宋体">