如何使用GridView控件AutoGenerateDeletebutton [英] How to use the GridView AutoGenerateDeletebutton
问题描述
我使用Visual Studio 2010中使用C#开发一个asp.net应用程序。我通过以下创建的GridView的表
I am using Visual Studio 2010 to develop an asp.net app using c#. I created an GridView table by the following
<asp:GridView ID="GridView1" runat="server" AutoGenerateDeleteButton="True"
EnableViewState="False" OnRowDeleting="DeleteRowButton_Click">
</asp:GridView>
但我不知道如何使用自动生成的在我的C#code删除键。
But I do not know how to use the auto generated delete button in my c# code.
我在网上搜索,他们总是提供我的code作为
I search online, they always provide my code as
protected void DeleteRowButton_Click(Object sender, GridViewDeleteEventArgs e)
{
var PN = GridView1.DataKeys[e.RowIndex].Values["Part_Number"];
string PN = pn.ToString;
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["XMLConnectionString"].ConnectionString);
// Create the command object
con.Open();
string str = "DELETE * FROM XML WHERE ([Part_Numbber] = " + PN + ")";
SqlCommand cmd = new SqlCommand(str, con);
cmd.ExecuteNonQuery();
Button1_Click(sender, e);
con.Close();
}
非常感谢你对任何人能告诉我该怎么做。
Thank you very much for anyone can tell me how to do it
推荐答案
有关删除所有记录从如果您有任何独特的或主键。如果要使用场Part_Numbber删除记录,那么这个字段的数据类型应该是int或BIGINT数据库中的表。那么现在把下面的code删除。
For Delete any record from you should have any unique or Primary key. If you want to delete record using field "Part_Numbber" then this field data-type should be of either int or bigint in Database Table. Then Now put the following code to Delete.
protected void DeleteRowButton_Click(Object sender, GridViewDeleteEventArgs e)
{
int Part_Numbber= Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value);
SqlCommand cmd = new SqlCommand("DELETE FROM XML WHERE Part_Numbber=" + Part_Numbber+ "", con);
con.Open();
int temp = cmd.ExecuteNonQuery();
if (temp == 1)
{
lblMessage.Text = "Record deleted successfully";
}
con.Close();
FillGrid();
}
这怎么可能会帮助你。
How this may help you.
这篇关于如何使用GridView控件AutoGenerateDeletebutton的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!