Я создал функцию Delete, которая устанавливает «Состояние» элемента GridView в 0. Изменения отражаются в БД SQL, однако запись по-прежнему отображается в 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>