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

时间:2021-09-23 06:23:56

I am making an ordering website as part of an assignment. A doctor logs in and sees all the orders put forward by his patients. When the doctor clicks some check boxes and clicks the button - the boxes that are checked update the orders (rows) column (approve) to approved. The unchecked boxes update the approve column to disapproved - this all works and updates...

我正在制作一个订购网站作为作业的一部分。一个医生登录进来,看到他的病人提出的所有命令。当医生单击一些复选框并单击按钮时——选中的方框将更新orders(行)列(approve)更新为approved。未选中的框将审批列更新为不批准——这一切都可以工作和更新……

however the grid does not disappear after this - the doctor can keep updating the orders and changing them but I want only the new orders that have not been checked/unchecked to appear. - I dont know how to apply this to my vb code.

但是网格不会在此之后消失——医生可以继续更新订单并更改它们,但我只希望出现未检查/未检查的新订单。-我不知道如何把这个应用到我的vb代码中。

I dont want the orders to delete from the database order table - just present the new orders to the doctor that he has not yet checked/unchecked.

我不希望订单从数据库订单表中删除——只需将新订单提交给医生,而医生还没有检查/检查。

my gird:

我的准备:

    <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>

my code behind gid:

gid背后的代码:

Imports System.Data.SqlClient

Imports System.Data

进口System.Data

Partial Class Pages_docorders Inherits System.Web.UI.Page

部分类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

End Class

结束课

Hopefully someone can help - thank you :)

希望有人能帮忙——谢谢你:)

1 个解决方案

#1


1  

If the Approved field is NULL in Order_pres as long as no decision has been made about the order approval, you could try this:

如果批准的字段在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

The query looks for records in docgridview for which the corresponding record in Order_pres has not been approved or disapproved yet.

查询在docgridview中查找尚未批准或不批准Order_pres中的相应记录的记录。

N.B. The code above assumes that the following lines are present at the top of your VB code file:

注意:上面的代码假设在VB代码文件的顶部有以下几行代码:

Imports System.Data
Imports System.Data.SqlClient

#1


1  

If the Approved field is NULL in Order_pres as long as no decision has been made about the order approval, you could try this:

如果批准的字段在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

The query looks for records in docgridview for which the corresponding record in Order_pres has not been approved or disapproved yet.

查询在docgridview中查找尚未批准或不批准Order_pres中的相应记录的记录。

N.B. The code above assumes that the following lines are present at the top of your VB code file:

注意:上面的代码假设在VB代码文件的顶部有以下几行代码:

Imports System.Data
Imports System.Data.SqlClient