I have a DataGridView populated with a SQL statement that users can input data on certain columns:
Me.bndDataGrid.DataSource = GetData("SELECT H.InnCode, R.RSRM, " & strCols & " E.Escalation " & _
"FROM (((dbo_HotelInfo AS H " & _
"INNER JOIN dbo_RSRM AS R ON H.RevMgr = R.ID) " & _
"INNER JOIN dbo_SrMgr AS S ON R.SrMgr = S.ID) " & _
"INNER JOIN " & strHitList & " AS L ON H.FacilityID = L.FacilityID) " & _
"INNER JOIN dbo_Escalation AS E ON H.FacilityID = E.FacilityID " & _
"WHERE S.ID = " & cbxSrMgr.SelectedValue.ToString)
With Me.grdQueryResults
.AutoGenerateColumns = True
.DataSource = bndDataGrid
End With
bndDataGrid is the BindingSource for grdQueryResults, the DataGridView. The code for GetData is commonly found on MS forums:
Private Shared Function GetData(ByVal sqlCommand As String) As DataTable
Dim strConn As String = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source={MS Access DB Path Here};"
Dim ctnHitList As OleDbConnection = New OleDbConnection(strConn)
Dim tblHitList As New DataTable
Dim cmdHitList As New OleDbCommand(sqlCommand, ctnHitList)
Dim adrHitList As OleDbDataAdapter = New OleDbDataAdapter()
adrHitList.SelectCommand = cmdHitList
tblHitList.Locale = System.Globalization.CultureInfo.InvariantCulture
adrHitList.Fill(tblHitList)
Return tblHitList
End Function
Now once users are ready to save changes I can't for the life of me figure out how to have this save correctly, mostly since the data source for the DataGridView isn't simply bound to a table.
EDIT:
OK, so I mostly overhauled my code in accordance with Crowcoder's blog page, and got a lot further with it, but now on updating I'm running into a "Concurrency violation: the UpdateCommand affected 0 of the expected 1 records" exception. Here's the update code:
Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
Dim parInnCode As OleDbParameter = New OleDbParameter("@parInnCode", OleDbType.WChar)
Dim parNotes As OleDbParameter = New OleDbParameter("@parNotes", OleDbType.WChar)
parInnCode.SourceColumn = "InnCode"
parNotes.SourceColumn = "Notes"
Using ctnDataGrid As New OleDbConnection(getConnectionString())
Using cmdGrid As New OleDbCommand("UPDATE (dbo_The400 AS T INNER JOIN dbo_HotelInfo AS H ON T.FacilityID = H.FacilityID) " & _
"INNER JOIN dbo_RSRM AS R ON H.RevMgr = R.ID " & _
"SET [Notes] = @parNotes WHERE H.InnCode = @parInnCode", ctnDataGrid)
Using adrDataGrid As New OleDbDataAdapter()
With adrDataGrid
.UpdateCommand = cmdGrid
With .UpdateCommand.Parameters()
.Add(parInnCode)
.Add(parNotes)
End With
grdQueryResults.EndEdit()
.Update(tblDataGrid)
End With
End Using
End Using
End Using
End Sub
tblDataGrid is declared at the Form Class level, wondering if that may be the issue or if my update query doesn't match the number of columns in the table? Or something else? Can't seem to find the right answer for my case :/
Welp, at long last I figured it out. It involved quite a bit of back end reorganizing to make it easier to connect to it via the front end .net application. Basically I threw all manager inputs into one table and added a column that corresponds to the initiative that required their input.
From there, I could build just about everything in the Dataset Designer and set the Update command on that tableadapter to just update that one table. Cut down the code on the main form to just a few lines that fill the datasources for the combobox columns in the datagridview.
Not an elegant coding solution, but it works!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.