C#识别gridview中的行以便删除

时间:2016-08-22 18:51:06

标签: c# .net gridview row access

我正在开发一个项目,我将在其中显示,插入,删除访问数据库中的数据。我制作了一些按钮,用于显示数据库中的特定表格。然后我创建了一个asp列来显示每行旁边的删除按钮。我想弄清楚的问题是:我怎么能拥有它,以便在单击删除按钮时识别特定行,然后我可以删除它?欢迎任何提示或提示。谢谢。

<body>
 <h1 class="center" style="text-align: center" >Display, Delete, and Add</h1>
<h3 id="Title1"></h3>
<style>
.center{
    margin: 0 auto;
}

</style>

<form id="form1"  runat="server">
    <asp:GridView ID="GridView1" class="center" runat="server" Width="300px" >
        <Columns>
            <asp:TemplateField>
                <ItemTemplate>
                    <asp:Button ID="btn1" Text="Delete" runat="server" OnClick="btn1_Click"/>
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>
    <p>
        &nbsp;</p>
    <p >
        <asp:Button ID="Button1" runat="server" OnClick="Button1_Click1" Text="Courses"  />
        <asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="Student Information" />
        <asp:Button ID="Button3" runat="server" OnClick="Button3_Click" Text="Students" />
    </p>
    <p>
        &nbsp;</p>
    <p>
        &nbsp;</p>
    <p>
        &nbsp;</p>
</form>

public partial class _Default : System.Web.UI.Page
{
    OleDbConnection con = new OleDbConnection  (@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\achowdhary\Documents\Database1.accdb");
protected void Page_Load(object sender, EventArgs e)
{

}


protected void Button1_Click1(object sender, EventArgs e)
{


    OleDbDataAdapter ada = new OleDbDataAdapter(" SELECT * FROM COURSES", con);
    DataSet set = new DataSet();
    ada.Fill(set, "COURSES");

    DataTable tab = new DataTable();
    tab = set.Tables["COURSES"];

    GridView1.DataSource = tab;
    GridView1.DataBind();



}

protected void Button2_Click(object sender, EventArgs e)
{
    OleDbDataAdapter ada = new OleDbDataAdapter(" SELECT * FROM STUDENT_INFO", con);
    DataSet set = new DataSet();
    ada.Fill(set, "STUDENT_INFO");

    DataTable tab = new DataTable();
    tab = set.Tables["STUDENT_INFO"];

    GridView1.DataSource = tab;
    GridView1.DataBind();
}

protected void Button3_Click(object sender, EventArgs e)
{
    OleDbDataAdapter ada = new OleDbDataAdapter(" SELECT * FROM STUDENTS", con);
    DataSet set = new DataSet();
    ada.Fill(set, "STUDENTS");

    DataTable tab = new DataTable();
    tab = set.Tables["STUDENTS"];

    GridView1.DataSource = tab;
    GridView1.DataBind();
}

protected void btn1_Click(object sender, EventArgs e)
{
    //ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('" + "hello" + "');", true);

}

}

1 个答案:

答案 0 :(得分:0)

我喜欢的许多可能性是,使用您的标识符绑定commandArgument in button,并在post back上恢复此值

    protected void btn1_Click(object sender, EventArgs e)
    {
        var identifier = ((Button)sender).CommandArgument;
    }

HTML

 <ItemTemplate>
     <asp:Button ID="btn1" Text="Delete" runat="server" OnClick="btn1_Click" CommandArgument='<%# Eval("PropertyName") %>'/>
 </ItemTemplate>