简体   繁体   English

在GridView中删除不起作用

[英]Deleting in gridview not working

i designed a page for admin to search users from database table using a Get button and delete users in gridview 我设计了一个页面,供管理员使用“获取”按钮从数据库表中搜索用户并在gridview中删除用户

This my source coding.. 这是我的源代码。

                <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" AutoGenerateDeleteButton="True" DataKeyNames="username"  EnableModelValidation="True" OnRowDeleting="GridView1_RowDeleting" OnSelectedIndexChanged="GridView1_SelectedIndexChanged1">
                    <Columns>
                        <asp:BoundField DataField="Id" HeaderText="Id" InsertVisible="False" ReadOnly="True" SortExpression="Id" />
                        <asp:BoundField DataField="name" HeaderText="name" SortExpression="name" />
                        <asp:BoundField DataField="adm" HeaderText="adm" SortExpression="adm" />
                        <asp:BoundField DataField="mobno" HeaderText="mobno" SortExpression="mobno" />
                        <asp:BoundField DataField="branch" HeaderText="branch" SortExpression="branch" />
                        <asp:BoundField DataField="year" HeaderText="year" SortExpression="year" />
                        <asp:BoundField DataField="username" HeaderText="username" ReadOnly="True" SortExpression="username" />
                        <asp:BoundField DataField="password" HeaderText="password" SortExpression="password" />
                        <asp:BoundField DataField="usertype" HeaderText="usertype" SortExpression="usertype" />
                    </Columns>
                </asp:GridView>
                <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:CanteenConnectionString %>" SelectCommand="SELECT * FROM [Hosteller]" DeleteCommand="DELETE FROM Hosteller WHERE (username = @username)">
                    <DeleteParameters>
                        <asp:Parameter Name="username" />
                    </DeleteParameters>
                </asp:SqlDataSource>
            </td>
        </tr>
        <tr>
            <td>&nbsp;</td>
        </tr>
        <tr>
            <td>&nbsp;</td>
        </tr>
    </table>

</form>

 protected void btnget_Click(object sender, EventArgs e)
        {
            DataTable dt = new DataTable();
            string ConString = "Data Source=sheikha-pc\\sqlexpress;Initial Catalog=Canteen;Integrated Security=True";
            SqlConnection con = new SqlConnection(ConString);
            string qry = "select id,name,adm,mobno,branch,year,username,password,usertype from Hosteller where adm='" + txtsearch1.Text + "'";
            SqlDataAdapter adpt = new SqlDataAdapter(qry, con);
            adpt.Fill(dt);
            if (dt.Rows.Count == 0)
            {
                GridView1.EmptyDataText = "No data found";
                GridView1.DataBind();
            }
            else
            {
                GridView1.DataSource = dt;
                GridView1.DataBind();
            }

        }

this is the coding that i wrote for GET button 这是我为GET按钮编写的代码

the problem is when i click on delete it doesnt work! 问题是当我单击删除时它不起作用! please help......... 请帮忙.........

It looks like the issue here is that you are overriding the data source in your code behind. 看起来这里的问题是您在后面的代码中覆盖了数据源。 If you comment out the code in "btnget_Click" and add the datasource to GridView1 it should work to delete. 如果注释掉“ btnget_Click”中的代码并将数据源添加到GridView1,则应该删除它。

            <asp:GridView ID="GridView1" DataSourceID="SqlDataSource1" runat="server" AutoGenerateColumns="False" AutoGenerateDeleteButton="True" DataKeyNames="username"  EnableModelValidation="True" OnRowDeleting="GridView1_RowDeleting" OnSelectedIndexChanged="GridView1_SelectedIndexChanged1">
                <Columns>
                    <asp:BoundField DataField="Id" HeaderText="Id" InsertVisible="False" ReadOnly="True" SortExpression="Id" />
                    <asp:BoundField DataField="name" HeaderText="name" SortExpression="name" />
                    <asp:BoundField DataField="adm" HeaderText="adm" SortExpression="adm" />
                    <asp:BoundField DataField="mobno" HeaderText="mobno" SortExpression="mobno" />
                    <asp:BoundField DataField="branch" HeaderText="branch" SortExpression="branch" />
                    <asp:BoundField DataField="year" HeaderText="year" SortExpression="year" />
                    <asp:BoundField DataField="username" HeaderText="username" ReadOnly="True" SortExpression="username" />
                    <asp:BoundField DataField="password" HeaderText="password" SortExpression="password" />
                    <asp:BoundField DataField="usertype" HeaderText="usertype" SortExpression="usertype" />
                </Columns>
            </asp:GridView>

And the code behind: 以及后面的代码:

protected void btnget_Click(object sender, EventArgs e)
        {


        }

This might not be what you want though. 这可能不是您想要的。 This will load the data on page load so the user will not need to click the button in order to get the results. 这将在页面加载时加载数据,因此用户无需单击按钮即可获取结果。

If you want the user to click the button then you will need to rewirte your code a bit and put some code in RowDeleting() as @Cal279 sugested in his comment. 如果您希望用户单击按钮,则需要重新编写代码,并将一些代码放入RowDeleting()中,因为@ Cal279在其注释中带有注释。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM