laurajs laurajs - 4 months ago 33
Vb.net Question

How to filter data/conect on a grid view

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

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.

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:

Imports System.Data.SqlClient


Imports System.Data

Partial Class Pages_docorders
Inherits 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 :)

Answer

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:

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.

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

Imports System.Data
Imports System.Data.SqlClient
Comments