如何在网格视图上过滤数据/ conect

时间:2016-07-22 14:42:27

标签: asp.net vb.net visual-studio

我正在订购网站作为作业的一部分。医生登录并查看患者提出的所有命令。当医生点击一些复选框并单击按钮时 - 检查的框将订单(行)列(批准)更新为已批准。未选中的框会更新批准列以拒登 - 这一切都可以正常运行并更新......

然而,在此之后网格不会消失 - 医生可以继续更新订单并更改它们,但我只想要未检查/未检查的新订单出现。 - 我不知道如何将其应用到我的vb代码中。

我不希望从数据库订单表中删除订单 - 只是向医生提供他尚未检查/取消选中的新订单。

我的网格:

    <asp:GridView ID="GridViewdoc" runat="server" AutoGenerateColumns="False" DataKeyNames="OrderId">
     <Columns>
  <asp:BoundField DataField="OrderID" HeaderText="Order Id" />
  <asp:BoundField DataField="DoctorId" HeaderText="Doctor Id" />
  <asp:BoundField DataField="Forename" HeaderText="Forename" />
  <asp:BoundField DataField="Surname" HeaderText="Surname" />
  <asp:BoundField DataField="MedicineId" HeaderText="Medicine Id" />
  <asp:BoundField DataField="MedicineName" HeaderText="Medicine Name" />
  <asp:BoundField DataField="pharmname" HeaderText="Pharmacy Name" />
  <asp:BoundField DataField="Dateordered" HeaderText="Date Ordered" />
  <asp:TemplateField HeaderText="Approve Status"> 
  <ItemTemplate> 
    <asp:CheckBox   ID="ApproveBox" runat="server"  /> 
  </ItemTemplate> 
   </asp:TemplateField>
     </Columns>
 </asp:GridView>

我的代码背后是gid:

Imports System.Data.SqlClient

Imports System.Data

Partial Class Pages_docorders     继承System.Web.UI.Page

Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load

    If Not IsPostBack Then
        Dim conn As New System.Data.SqlClient.SqlConnection("Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\surgerydb.mdf;Integrated Security=True;Connect Timeout=30")
        Dim cmd3string As String = " Select * From docgridview  WHERE DoctorId = " & Session("DoctorId")
        Dim dt As New System.Data.DataTable()
        Dim da As New System.Data.SqlClient.SqlDataAdapter(cmd3string, conn)
        conn.Open()
        da.Fill(dt)
        conn.Close()



        GridViewdoc.DataSource = dt
        GridViewdoc.DataBind()

    End If

End Sub

Protected Sub GridViewdoc_RowDataBound(sender As Object, e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridViewdoc.RowDataBound


    If e.Row.RowType = DataControlRowType.DataRow Then
        Dim drview As DataRowView = TryCast(e.Row.DataItem, DataRowView)

        'Find checkbox and checked/Unchecked based on values
        Dim chkb As CheckBox = DirectCast(e.Row.FindControl("ApproveBox"), CheckBox)
        If drview(8).ToString() = "Approve" Then
            chkb.Checked = True
        Else
        End If
    End If

End Sub
Protected Sub btnapprove_Click(sender As Object, e As System.EventArgs) Handles btnapprove.Click
    Dim dt As Data.DataTable = Session("Approved")
    Dim val As String
    val = ""
    Dim Oid As Integer
    Dim conn As New System.Data.SqlClient.SqlConnection("Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\surgerydb.mdf;Integrated Security=True;Connect Timeout=30")

    For Each row As GridViewRow In GridViewdoc.Rows
        Dim therowindex As Integer = row.RowIndex
        Oid = Integer.Parse(GridViewdoc.DataKeys(therowindex).Value.ToString())
        val = ""
        Dim cb As CheckBox = row.FindControl("ApproveBox")
        If cb.Checked Then

            val = "Approved"
        Else

            val = "Disapproved"
        End If
        If Oid > 0 Then
            Dim cmdstring As String = " UPDATE Order_pres SET Approved = @appr  Where OrderID= @oid"
            conn.Close()
            conn.Open()
            Dim cmd = New SqlCommand(cmdstring, conn)
            cmd.Parameters.Add("@appr", Data.SqlDbType.NVarChar).Value = val
            cmd.Parameters.Add("@oid", Data.SqlDbType.NVarChar).Value = Oid
            Dim result As Integer
            result = cmd.ExecuteNonQuery()

        End If
    Next

    Dim cmd3string As String = " Select * From docgridview  WHERE DoctorId = " & Session("DoctorId")
    Dim dtm As New System.Data.DataTable()
    Dim da As New System.Data.SqlClient.SqlDataAdapter(cmd3string, conn)
    conn.Close()
    conn.Open()
    da.Fill(dtm)
    conn.Close()
    GridViewdoc.DataSource = dtm
    GridViewdoc.DataBind()
End Sub

结束班

希望有人可以提供帮助 - 谢谢:)

1 个答案:

答案 0 :(得分:1)

如果Approved中的NULL字段为Order_pres,只要未就订单批准做出任何决定,您可以尝试以下操作:

Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
    If Not IsPostBack Then
        BindGridViewData()
    End If
End Sub

Protected Sub btnapprove_Click(sender As Object, e As System.EventArgs) Handles btnapprove.Click
    ...
    For Each row As GridViewRow In GridViewdoc.Rows
        ...
    Next
    BindGridViewData()
End Sub

Protected Sub BindGridViewData()
    Using conn As New SqlConnection("Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\surgerydb.mdf;Integrated Security=True;Connect Timeout=30")
        Using cmd As New SqlCommand("SELECT DGV.* FROM docgridview DGV INNER JOIN Order_pres OP ON OP.OrderID = DGV.OrderID WHERE DGV.DoctorId = @DoctorId AND OP.Approved IS NULL", conn)
            cmd.CommandType = CommandType.Text
            cmd.Parameters.Add(New SqlParameter("@DoctorId", Session("DoctorId")))
            Dim dataAdapter As New SqlDataAdapter(cmd)
            Dim dtm As New DataTable()
            dataAdapter.Fill(dtm)
            GridViewdoc.DataSource = dtm.DefaultView
            GridViewdoc.DataBind()
        End Using
    End Using
End Sub

该查询会在docgridview中查找Order_pres中尚未批准或拒登的相应记录的记录。

N.B。上面的代码假定VB代码文件顶部有以下行:

Imports System.Data
Imports System.Data.SqlClient