除非指定了 DeleteCommand,否则数据源“AccessDataSource1”不支持删除操作。
<asp:GridView ID ="GridView1" runat ="server" AutoGenerateColumns ="False"
DataKeyNames ="购物流水号" DataSourceID ="AccessDataSource1" ShowFooter ="True"
CellPadding ="0" GridLines ="Horizontal" OnRowDataBound="rowdtbound"
RowStyle ="center" OnRowDeleting="GridView1_RowDeleting" OnRowCancelingEdit="GridView1_RowCancelingEdit">
<HeaderStyle Height="20px" HorizontalAlign="Center" VerticalAlign="Middle" ForeColor="White" BackColor="black" />
<Columns>
<asp:BoundField DataField="购物流水号" HeaderText="购物流水号" ReadOnly="True" SortExpression="购物流水号" />
<asp:BoundField DataField="用户名" HeaderText="用户名" SortExpression="用户名" />
<asp:BoundField DataField="产品编号" HeaderText="产品编号" SortExpression="产品编号" />
<asp:BoundField DataField="产品名称" HeaderText="产品名称" SortExpression="产品名称" />
<asp:BoundField DataField="产品单价" HeaderText="产品单价" SortExpression="产品单价" />
<asp:BoundField DataField="购买数量" HeaderText="购买数量" SortExpression="购买数量" />
<asp:CommandField HeaderText="删除" ShowDeleteButton="true" />
</Columns>
<RowStyle HorizontalAlign ="Center" />
</asp:GridView>
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
string cname = (GridView1.DataKeys[e.RowIndex].Value).ToString();
string OleDbStr = " delete from [用户购物表] where [购物流水号]=" + Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value.ToString()) + "";
OleDbConnection myConn = GetCon();
OleDbCommand OleDbcom = new OleDbCommand(OleDbStr, myConn);
myConn.Open();
OleDbcom.ExecuteNonQuery();
ClientScript.RegisterClientScriptBlock(this.GetType(), "info", "alert('记录已删除');", true);
myConn.Close();
GridViewBind();
}