简体   繁体   English

如何使用DataGridView更新数据库

[英]how to update database using DataGridView

Basically i have a datagridview which is filled by a query upon the loading of the page to display all the incomplete orders in my database table as below: 基本上我有一个datagridview,该查询由页面加载时的查询填充,以显示数据库表中所有不完整的订单,如下所示: 在此处输入图片说明

I was wondering if it's possible to allow the user to edit them so they can mark the incomplete orders as completed, i was thinking through either just allowing the column to be editable, or maybe a set of checkboxes alongside each row which would mark them as completed. 我想知道是否可以允许用户对其进行编辑,以便他们可以将未完成的订单标记为已完成,我在考虑要么只是允许该列是可编辑的,要么是在每行旁边设置一组复选框,将其标记为完成。

here's my current code the page: 这是我当前的代码页面:

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

If you wanted to edit the order_status field, you could set the the column as a DataGridViewComboBox which would contain all possible status values. 如果要编辑order_status字段,可以将列设置为DataGridViewComboBox,其中将包含所有可能的状态值。 Then create an event handler to update the database when the combo box value is changed. 然后创建一个事件处理程序,以在更改组合框值时更新数据库。

Edit: Code 编辑:代码

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

Edit 1: Found another possible answer: DataGridView Cell Editing and Updating DB (C#) 编辑1:找到另一个可能的答案: DataGridView单元格编辑和更新数据库(C#)

Edit 2: Changed sql.RunQuery to a SqlCommand object. 编辑2:将sql.RunQuery更改为SqlCommand对象。

This is how I do it. 这就是我的方法。 This is hooked to a dropdown list but it can be hooked to any control in the grid: 这被挂钩到一个下拉列表,但是可以被挂钩到网格中的任何控件:

' 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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM