gpt4 book ai didi

c# - 在 gridview 中删除不起作用

转载 作者:行者123 更新时间:2023-11-30 22:34:26 25 4
gpt4 key购买 nike

我为管理员设计了一个页面,使用“获取”按钮从数据库表中搜索用户并在 gridview 中删除用户

这是我的源代码..

                <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();
}

}

这是我为 GET 按钮编写的代码

问题是当我点击删除时它不起作用!请帮忙…………

最佳答案

看起来这里的问题是您在后面的代码中覆盖了数据源。如果您注释掉“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>

以及背后的代码:

protected void btnget_Click(object sender, EventArgs e)
{


}

但这可能不是您想要的。这将在页面加载时加载数据,因此用户无需单击按钮即可获得结果。

如果您希望用户单击按钮,那么您将需要稍微重新编写代码并将一些代码放入 RowDeleting() 中,正如 @Cal279 在他的评论中所建议的那样。

关于c# - 在 gridview 中删除不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33017210/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com