我的GridView删除行时提示"除非指定了 DeleteCommand,否则数据源“SqlDataSource1”不支持删除操作。 ",但是删除是有效的.想问一下这问题该如何解决,谢谢先:)
删除行的函数如下:
protected void newsGrid_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
DataKey key = newsGrid.DataKeys[e.RowIndex];
int id = int.Parse(key[0].ToString());
using (SqlConnection conn = AdoSql.CreateConn())
{
conn.Open();
string SqlString = "delete from [news] where newsid=" +id;
SqlCommand command = new SqlCommand(SqlString, conn);
command.ExecuteNonQuery();
newsGrid.DataBind();
}
} 下面是删除按钮页面文件的代码
<asp:TemplateField HeaderText="删除新闻">
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False"
CommandName="Delete" Text="删除"
OnClientClick="return confirm('确认删除该记录?');"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
下面是页面与GridView绑定的数据源的代码
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:database %>"
SelectCommand="SELECT [newsid], [newstitle], [newstime], [newscount] FROM [news]">
</asp:SqlDataSource>
删除行的函数如下:
protected void newsGrid_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
DataKey key = newsGrid.DataKeys[e.RowIndex];
int id = int.Parse(key[0].ToString());
using (SqlConnection conn = AdoSql.CreateConn())
{
conn.Open();
string SqlString = "delete from [news] where newsid=" +id;
SqlCommand command = new SqlCommand(SqlString, conn);
command.ExecuteNonQuery();
newsGrid.DataBind();
}
} 下面是删除按钮页面文件的代码
<asp:TemplateField HeaderText="删除新闻">
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False"
CommandName="Delete" Text="删除"
OnClientClick="return confirm('确认删除该记录?');"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
下面是页面与GridView绑定的数据源的代码
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:database %>"
SelectCommand="SELECT [newsid], [newstitle], [newstime], [newscount] FROM [news]">
</asp:SqlDataSource>
protected void newsGrid_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
int id = Convert.ToInt32(newsGrid.DataKeys[e.RowIndex].Value.ToString());
using (SqlConnection conn = AdoSql.CreateConn())
{
conn.Open();
string SqlString = "delete from [news] where newsid=" +id;
SqlCommand command = new SqlCommand(SqlString, conn);
command.ExecuteNonQuery();
}
newsGrid.DataBind();
}
参考