我创建了一个Delete函数,该函数将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>
答案 0 :(得分:0)
您需要像这样将数据源设置为您的gvPerson:
Dim datatable = dsPerson.Tables("Personnel")
gvPerson.DataSource = datatable
gvPerson.DataBind()
希望有帮助。
答案 1 :(得分:0)
我发现了如何通过在GridView下添加以下代码来解决问题的方法:
<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:SecurityDBConnectionString2 %>" SelectCommand="SELECT [SempID], [EmpName], [PDNo], [Position], [DateHired], [ContactNo], [Email], [EmergencyContactNo], [DateQuitTerminated], [ContactPerson], [Remarks], [Status] FROM [Personnel] WHERE ([Status] = @Status)">
<SelectParameters>
<asp:Parameter DefaultValue="1" Name="Status" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>