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

Posted

技术标签:

【中文标题】C#在gridview中识别行以便删除【英文标题】:C# Identifying the Row in a gridview inorder to Delete 【发布时间】:2016-08-22 18:51:05 【问题描述】:

我正在做一个项目,我将在其中显示、插入、删除我的访问数据库中的数据。我制作了显示数据库中特定表的按钮。然后我创建了一个 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】:

许多可能性,我更喜欢将按钮中的 commandArgument 与您的标识符绑定,并在回发时恢复此值

    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>

【讨论】:

以上是关于C#在gridview中识别行以便删除的主要内容,如果未能解决你的问题,请参考以下文章

从 C# 添加新行时如何在 GridView 中添加删除链接?

c# asp.net GridView中如何删除一条记录后刷新页面

C#如何修改dategridview里头数据

c# datagridview 删除行

c#在datagridview中删除指定行并更新到数据库

C#中datagridview控件 如何进行删除修改添加数据并保存到数据库中