【问题标题】:add a column of checkboxes to a grid that will update a field in a database将一列复选框添加到将更新数据库中的字段的网格
【发布时间】:2016-07-13 13:14:07
【问题描述】:

我有一个 GridView 保存我的订单表,该表出现在我的文档页面上(这些是插入语句中的插入值) - 这是选择语句:

Create View docgridview 
As 

Select A.OrderID
  ,A.DoctorId,B.Forename,B.Surname ,A.MedicineId,C.Name      as      MedicineName,D.pharmname,A.Dateordered, Approved
From  order_pres  A
Left  Join Patient   B on (A.PatientId  = B.PatientId)
Left  Join Medicine  C on (A.MedicineId = C.MedicineId)
Left  Join pharmacy  D on (A.PharmacyId   = D.PharmacyId)
Left  Join Doctor    E on (A.DoctorId  = E.DoctorId)

然后使用Docgridview将语句绑定到gridview:

 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

如何将一列复选框添加到 GridView 中,如果选中按钮,则更新从 order_pres 'approved' 和 'disapproved' 批准的列。点击

网格视图:

<asp:GridView ID="GridViewdoc" runat="server"  >

</asp:GridView>


<asp:Button ID="btnapprove" runat="server" Text="Button" />

要更新的表:

【问题讨论】:

  • 在您做任何其他事情之前,您应该阅读、理解并开始使用参数化查询。该会话实际上非常安全,但那里的查询模式表明 sql 注入很可能出现在系统中的其他位置。至于实际问题,我有点困惑。您声明您想要一个复选框列,然后谈论单击按钮?我认为只有复选框就足够了。然后为 onclick 事件设置一个事件处理程序。
  • 在你看到的网格中会有一些订单按钮。点击确认
  • 嗯?看到按钮是什么意思?你不需要一个按钮,你只需要一个复选框。
  • Sean,网格上将有多个订单 - 一旦选中,我需要复选框执行的操作是将已批准的列更新为已批准的值 - 如果没有检查,则它将更新未批准按钮。点击
  • 我希望有超过 1 个订单。这就是为什么我说你需要在网格中有一个复选框。并将事件处理程序添加到复选框以更新当前行的数据。当托运属性的状态更改时,它将更新行。 span>

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


【解决方案1】:

您需要手动定义网格视图中的列:

<asp:GridView ID="GridViewdoc" runat="server" AutoGenerateColumns="False">
      <Columns>
           <asp:BoundField DataField="OrderID"></asp:BoundField>
           <asp:BoundField DataField="DoctorId"></asp:BoundField>
           <asp:BoundField DataField="Forename"></asp:BoundField>
           <asp:BoundField DataField="Surname"></asp:BoundField>
           <asp:BoundField DataField="MedicineId"></asp:BoundField>
           <asp:BoundField DataField="MedicineName"></asp:BoundField>
           <asp:BoundField DataField="pharmname"></asp:BoundField>
           <asp:BoundField DataField="Dateordered"></asp:BoundField>
           <asp:BoundField DataField="Approved"></asp:BoundField>
           <asp:TemplateField>
               <ItemTemplate>
                   <asp:CheckBox Text="Approve" ID="ApproveBox" runat="server" />
               </ItemTemplate>
           </asp:TemplateField>
      </Columns>
</asp:GridView>

最后一列是复选框列。为 GridViewdoc.RowDataBound 添​​加一个处理程序以将框设置为选中(因为看起来您将它们全部默认为已批准)

Protected Sub GridViewdoc_RowDataBound(sender As Object, e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridViewdoc.RowDataBound
    If e.Row.RowType = DataControlRowType.DataRow Then
        CType(e.Row.FindControl("ApproveBox"), Checkbox).Checked = True
    End If
End Sub

然后在您的按钮单击事件处理程序中处理选中和未选中的框。至于更改数据,您将需要使用会话或缓存变量来存储数据表,以便您可以修改其数据并重新绑定网格视图与更改的数据。

Protected Sub btnapprove_Click(sender As Object, e As System.EventArgs) Handles btnapprove.Click
    Dim dt As Data.DataTable = Session("YourDataTableVariable") 
    For Each row As GridViewRow In GridViewdoc.Rows
        Dim cb As CheckBox = row.FindControl("ApproveBox")
        If cb.checked Then
            dt.Rows(row.RowIndex).Item(8) = "Approved"
        Else
            dt.Rows(row.RowIndex).Item(8) = "Disapproved"
        End If
    Next
    GridViewdoc.DataSource = dt
    GridViewdoc.DataBind()
End Sub

【讨论】:

  • 非常感谢您抽出宝贵的时间来回答这个问题,我正要测试这个 - 关于已批准的 bit 设置,如果这是要更新的列,我应该更改它吗这些价值观?谢谢你:)
  • 您可以将该位更改为字符串或继续使用该位并更改我提供的代码以将其设置为正确的值。
  • 嗨@wenadin-checked=true 是否有任何原因不起作用?谢谢你:)
  • 我编辑了我的那部分答案以解决问题。
  • 嗨@wenadin - 我在.checked 中收到此错误 System.Web.dll 中出现“System.ArgumentOutOfRangeException”类型的异常,但未在用户代码中处理其他信息:指定的参数已失效的有效值范围。
猜你喜欢
  • 2014-03-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-05-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多