参考这个。。我已经测试过了。。
你必须记住的事情..
你必须为你的两个gridview创建两个不同的表。但这些表设计应该与tbl1和tbl2相同。在tbl1中将一个字段作为主键并使其自动递增
不要在 tbl2 中保留任何主键或自动递增字段
这里在接受按钮上,首先数据将被插入到 tbl2,然后从原始表 tbl1 page.aspx 文件中删除
<asp:GridView ID="PendingRecordsGridview" runat="server"
AutoGenerateColumns="False" DataKeyNames="id"
onrowcommand="PendingRecordsGridview_RowCommand">
<Columns>
<asp:TemplateField HeaderText="Accept">
<ItemTemplate>
<asp:Button CommandArgument='<%# Bind("id") %>' ID="Button1" runat="server" CausesValidation="false"
CommandName="accept" Text="Accept" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="name" SortExpression="name">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("name") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("name") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="id" SortExpression="id">
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("id") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("id") %>'></asp:Label>
</EditItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="sd1" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
SelectCommand="SELECT * FROM [tbl1]"></asp:SqlDataSource>
现在 page.aspx.cs 文件
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
bind();
}
}
protected void bind()
{
PendingRecordsGridview.DataSource = sd1;
PendingRecordsGridview.DataBind();
}
protected void PendingRecordsGridview_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "accept")
{
Session["id"] = e.CommandArgument.ToString();
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
con.Open();
SqlCommand cmd1 = new SqlCommand("INSERT INTO tbl2 (id, name) SELECT id, name FROM tbl1 where id='"+Session["id"].ToString()+"'", con);
SqlCommand cmd2 = new SqlCommand("delete from tbl1 where id='"+Session["id"].ToString()+"'", con);
cmd1.ExecuteNonQuery();
cmd2.ExecuteNonQuery();
bind();
}
}
现在只需再翻一页.. 并将其与 tbl2 绑定,这将有已批准的记录...
随时询问这个问题。如果你觉得它有帮助,请将其标记为答案