【问题标题】:how to update database using DataGridView如何使用 DataGridView 更新数据库
【发布时间】:2016-07-13 19:59:13
【问题描述】:

基本上我有一个datagridview,它在加载页面时由查询填充,以显示我的数据库表中的所有不完整订单,如下所示:

我想知道是否可以允许用户编辑它们以便他们可以将不完整的订单标记为已完成,我正在考虑是否允许该列可编辑,或者可能在每行旁边设置一组复选框将它们标记为已完成。

这是我当前页面的代码:

Public Class processorders
    Dim sql As New sqlcontrol

    Private Sub ammendorders_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        If sql.HasConnection = True Then
            sql.RunQuery("SELECT customers.customer_id, first_name, second_name, phone_number, date_ordered, order_total, collection_method FROM (Orders INNER JOIN Customers on orders.customer_id = customers.customer_id) WHERE order_status='In Progress'")
            If sql.sqldataset.Tables.Count > 0 Then
                dgvData.DataSource = sql.sqldataset.Tables(0)
            End If
        End If

    End Sub
    'above queries database to find all incomplete orders



    Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
        Me.Close()
    End Sub

【问题讨论】:

  • 看起来订单状态应该是一个下拉菜单。 DGV 不更新数据库,它只是向用户显示数据并收集输入。您的数据库提供程序对象(如适配器)会进行更新。为了更新,您可能需要数据集中的 Id 列,但用户不需要看到它,如果您显示它,请确保它是 ReadOnly 列

标签: vb.net datagridview


【解决方案1】:

如果您想编辑 order_status 字段,您可以将该列设置为包含所有可能状态值的 DataGridViewComboBox。然后创建一个事件处理程序以在组合框值更改时更新数据库。

编辑:代码

Private Sub MyDataGridView_RowValidated(sender As Object, e As DataGridViewCellEventArgs) Handles MyDataGridView.RowValidated

  'update the data source here, could vary depending on the method used.
  if sql.HasConnection
    Dim sqlCmd as SqlCommand = new SqlCommand

    sqlCmd.connection = "<Your connection string>"
    sqlCmd.commandText = 
      String.format(
        "UPDATE Orders SET order_status='{0}' WHERE customer_id='{2}';",
        MyDataGridView.Rows(e.RowIndex).Cells(<order_status column index>).Value,
        MyDataGridView.Rows(e.RowIndex).Cells(<customer_id column index>).Value
      )
    sqlCmd.ExecuteNonQuery
  end if
End Sub

编辑 1:找到另一个可能的答案:DataGridView Cell Editing and Updating DB (C#)

编辑 2:将 sql.RunQuery 更改为 SqlCommand 对象。

【讨论】:

  • 您能否提供一些示例代码来帮助说明您的答案?
  • 我该怎么做呢?由于在页面加载之前列不存在,除非我遗漏了什么,否则我看不到任何更改列属性的方法?
  • 很高兴它有帮助。 :)
【解决方案2】:

这就是我的做法。这与下拉列表挂钩,但可以与网格中的任何控件挂钩:

' This enables you to capture the Update event on a gridview

Dim clickedRow As GridViewRow = TryCast(DirectCast(sender, DropDownList).NamingContainer, GridViewRow)


Dim cust_id As Label = DirectCast(clickedRow.FindControl("lbl_grd_id"), Label)
Dim status As DropDownList = DirectCast(clickedRow.FindControl("ddl_grd_crew_id"), DropDownList)


Dim cmd As New SqlCommand()
cmd.CommandText = "UPDATE [your_orders_table] set status = @status where customer_id = @cust_id "

cmd.Parameters.Add("@cust_id", SqlDbType.Int).Value = Convert.ToInt32(cust_id.Text)
cmd.Parameters.Add("@status", SqlDbType.VarChar).Value = status.Text

cmd.CommandType = CommandType.Text
cmd.Connection = Me.sqlConnection1
Me.sqlConnection1.Open()
'execute insert statement
cmd.ExecuteNonQuery()
Me.sqlConnection1.Close()
're-populate grid with a method call. if you don't the edits will not be reflected when the grid reloads 
fill_order_status_grd()

fill_order_status_grd.databind()

【讨论】:

  • 将此转换为 VB。我刚刚注意到您的原始代码是 VB 而不是 C#。
猜你喜欢
  • 1970-01-01
  • 2015-05-20
  • 1970-01-01
  • 1970-01-01
  • 2021-12-11
  • 1970-01-01
  • 1970-01-01
  • 2017-06-26
相关资源
最近更新 更多