Reputation: 39
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:
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
Upvotes: 1
Views: 3306
Reputation: 690
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()
Upvotes: 0
Reputation: 11
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. 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#)
Edit 2: Changed sql.RunQuery to a SqlCommand object.
Upvotes: 1