【发布时间】:2019-06-11 05:34:33
【问题描述】:
我创建了一个删除函数,它将 GridView 项目的“状态”设置为 0。更改反映在 SQL DB 中,但是,记录仍显示在 GridView 上。这是我的删除代码:
Protected Sub lbtnDeleteStaff_Click(ByVal sender As System.Object, ByVal e As EventArgs) Handles lbtnDeleteStaff.Click
Using conn As New SqlConnection(ConfigurationManager.ConnectionStrings("SecurityDBConnectionString2").ToString())
' Create a command object.
Dim cmd As New SqlCommand()
' Assign the connection to the command.
cmd.Connection = conn
' Set the command text
' SQL statement or the name of the stored procedure
cmd.CommandText = "UPDATE Personnel SET Status = 0 WHERE SempID = @SempID"
cmd.Parameters.AddWithValue("@SempID", xSelectedPersonID)
' Set the command type
' CommandType.Text for ordinary SQL statements;
' CommandType.StoredProcedure for stored procedures.
cmd.CommandType = CommandType.Text
' Get the PersonID of the selected row.
'Dim strSempID As String = gvPerson.Rows(e.RowIndex).Cells(2).Text
' Append the parameter.
'cmd.Parameters.Add("@SempID", SqlDbType.Int).Value = strSempID
' Open the connection.
conn.Open()
' Execute the command.
cmd.ExecuteNonQuery()
End Using
' Rebind the GridView control to show data after deleting.
BindGridView()
End Sub
这是我的 BindGridView 函数:
Private Sub BindGridView()
' Get the connection string from Web.config.
' When we use Using statement,
' we don't need to explicitly dispose the object in the code,
' the using statement takes care of it.
Using conn As New SqlConnection(ConfigurationManager.ConnectionStrings("DBConnectionString2").ToString())
' Create a DataSet object.
Dim dsPerson As New DataSet()
' Create a SELECT query.
Dim strSelectCmd As String = "SELECT SempID,EmpName,Position,PDNo,DateHired,ContactNo,Email,EmergencyContactNo,ContactPerson,DateQuitTerminated,Remarks FROM Personnel WHERE Status = 1"
' Create a SqlDataAdapter object
' SqlDataAdapter represents a set of data commands and a
' database connection that are used to fill the DataSet and
' update a SQL Server database.
Dim da As New SqlDataAdapter(strSelectCmd, conn)
' Open the connection
conn.Open()
' Fill the DataTable named "Personnel" in DataSet with the rows
' returned by the query.new n
da.Fill(dsPerson, "Personnel")
' Get the DataView from Security Personnel DataTable.
Dim dvPerson As DataView = dsPerson.Tables("Personnel").DefaultView
' Set the sort column and sort order.
'dvPerson.Sort = ViewState("SortExpression").ToString()
' Bind the GridView control.
'gvPerson.DataSource = dvPerson
gvPerson.DataBind()
conn.Close()
End Using
End Sub
我不太确定我的代码中遗漏了什么。我已经设置了Status=1所以 GridView 不应该只显示那些带有 1 的记录吗? GridView 当前显示状态为 1 和 0 的记录。我的目标是让带有 Status = 0 的记录从 GridView 中消失,但仍在数据库中。谢谢
问候,
编辑:这是 GridView 的代码。在代码的底部是我设置 SQLDataSource 的位置。
<asp:GridView ID="gvPerson" CssClass="EU_DataTable" runat="server" AutoGenerateColumns="False" DataKeyNames="SempID" DataSourceID="SqlDataSource2" AllowPaging="True" PageSize="6" AllowSorting="True" OnRowDataBound="gvPerson_RowDataBound" OnPageIndexChanging="gvPerson_PageIndexChanging" OnRowCommand="gvPerson_RowCommand">
<Columns>
<asp:CommandField ShowSelectButton="True" />
<asp:BoundField DataField="SempID" HeaderText="ID" InsertVisible="False" ReadOnly="True" SortExpression="SempID" />
<asp:BoundField DataField="EmpName" HeaderText="Name" SortExpression="EmpName" />
<asp:BoundField DataField="PDNo" HeaderText="Badge Number" SortExpression="PDNo" />
<asp:BoundField DataField="Position" HeaderText="Position" SortExpression="Position" />
<asp:BoundField DataField="DateHired" HeaderText="Date Hired" SortExpression="DateHired" />
<asp:BoundField DataField="ContactNo" HeaderText="Contact Number" SortExpression="ContactNo" />
<asp:BoundField DataField="Email" HeaderText="Email" SortExpression="Email" />
<asp:BoundField DataField="EmergencyContactNo" HeaderText="Emergency Contact Number" SortExpression="EmergencyContactNo" />
<asp:BoundField DataField="DateQuitTerminated" HeaderText="Termination Date" SortExpression="DateQuitTerminated" />
<asp:BoundField DataField="ContactPerson" HeaderText="Contact Person" SortExpression="ContactPerson" />
<asp:BoundField DataField="Remarks" HeaderText="Remarks" SortExpression="Remarks" />
</Columns>
<SelectedRowStyle BackColor="#54a1e5" Font-Bold="True" ForeColor="#CCFF99" />
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString2 %>" SelectCommand="SELECT [SempID], [EmpName], [PDNo], [Position], [DateHired], [ContactNo], [Email], [EmergencyContactNo], [DateQuitTerminated], [ContactPerson], [Remarks], [Status] FROM [Personnel]"></asp:SqlDataSource>
【问题讨论】:
标签: asp.net sql-server vb.net datagridview