I have a form set up to log project field notes.
It has 1 textbox 1 DGV, 1 DateTimePicker, 1 combobox, 5 buttons (see attachement for screenshot)
I have all the buttons wired up except I can't figure out how to add a row if the textbox was emply when it was loaded. As in when a user first opens the form they should be able to type in a note, hit OK btn and the new note saved. This requires adding a new row, i know. but the bindingsource.position is always 0 when it is first loaded, so what happens is I overwrite the note at position 0.
My new note btn will clear the txtbox and add a new row filled with the values of the txtbox, dtp, cmbobx. this seems to work ok.
The save btn just runs a endedit on the binding sources and a updateAll on the tableadapter. basic saving to database stuff.
btn OK just runs the same save function as the sa btn does, but it also closes the form.
I also have the txt box bound to the dataset so that if a user just needs to edit an existing note they click it in the DGV, edit the note and hit ok.
Here's my entire code for this form.
I know there are somethings in this code that seem wierd, and unnessary, that is becuase I have been trying alot of different thing out.
I hope someone can help.
It has 1 textbox 1 DGV, 1 DateTimePicker, 1 combobox, 5 buttons (see attachement for screenshot)
I have all the buttons wired up except I can't figure out how to add a row if the textbox was emply when it was loaded. As in when a user first opens the form they should be able to type in a note, hit OK btn and the new note saved. This requires adding a new row, i know. but the bindingsource.position is always 0 when it is first loaded, so what happens is I overwrite the note at position 0.
My new note btn will clear the txtbox and add a new row filled with the values of the txtbox, dtp, cmbobx. this seems to work ok.
The save btn just runs a endedit on the binding sources and a updateAll on the tableadapter. basic saving to database stuff.
btn OK just runs the same save function as the sa btn does, but it also closes the form.
I also have the txt box bound to the dataset so that if a user just needs to edit an existing note they click it in the DGV, edit the note and hit ok.
Here's my entire code for this form.
Code:
Public Class frmLog
Private ProjectID As Integer
Dim ProjectDS As OneDataDataSet
Dim ProjectRow As OneDataDataSet.BidRow
Sub New(ByVal ds As OneDataDataSet, ByVal BidID As Integer, Row As DataRow)
InitializeComponent()
ProjectID = BidID
ProjectDS = ds
ProjectRow = CType(Row, OneDataDataSet.BidRow)
End Sub
'Setup to dynamically create connection string from my.settings
Private OneRoofDatabaseCon As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & My.Settings.OneDataDir
Private Sub frmLog_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Use ConnectionStrings from the variable we set
Me.IDStaffTableAdapter.Connection.ConnectionString = OneRoofDatabaseCon
Me.BidLogTableAdapter.Connection.ConnectionString = OneRoofDatabaseCon
'TODO: This line of code loads data into the 'OneDataDataSet.IDStaff' table. You can move, or remove it, as needed.
Me.IDStaffTableAdapter.Fill(Me.OneDataDataSet.IDStaff)
'TODO: This line of code loads data into the 'OneDataDataSet.BidLog' table. You can move, or remove it, as needed.
Me.BidLogTableAdapter.Fill(Me.OneDataDataSet.BidLog)
'Set BidLog bindingSource filter to filter only our clients logs
BidLogBindingSource.Filter = String.Format("BidID = {0}", ProjectID)
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'Clear datagridview selection when form is loaded
If BidLogDataGridView.SelectedCells.Count > 0 Then
BidLogDataGridView.ClearSelection()
'clear Log Textbox when form is loaded
txtLogNote.Clear()
End If
Dim curLogNote As String
row = CType(CType(Me.BidLogBindingSource.Current, DataRowView).Row, OneDataDataSet.BidLogRow)
curLogNote = row.BidLog
'Set DateTimePicker to NOW
Me.dtpickLogged.Value = Now
'Set form Name property to Clients First Name, Last Name and Address
Me.Text = "Log Notes" + " - " + ProjectRow.FirstName + " " + ProjectRow.LastName + " - " + _
ProjectRow.StreetNo + " " + ProjectRow.Street
btnSaveNote.Enabled = False
End Sub
Private row As OneDataDataSet.BidLogRow
Private RI As Integer
Private Sub BidLogDataGridView_Click(sender As Object, e As System.EventArgs) Handles BidLogDataGridView.Click
btnDeleteNote.Enabled = True
btnOk.Enabled = False
btnSaveNote.Enabled = False
row = CType(CType(Me.BidLogBindingSource.Current, DataRowView).Row, OneDataDataSet.BidLogRow)
RI = BidLogDataGridView.CurrentRow.Index
txtLogNote.Text = row.BidLog
End Sub
Private Sub btnNewNote_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNewNote.Click
Me.txtLogNote.Clear()
dtpickLogged.Value = Now
btnSaveNote.Enabled = True
btnOk.Enabled = True
NewNote()
End Sub
Private Sub BtnClose_Click(sender As System.Object, e As System.EventArgs) Handles BtnCancel.Click
Me.Close()
End Sub
Public Sub SaveNote()
Me.Validate()
Me.IDStaffBindingSource.EndEdit()
Me.BidLogBindingSource.EndEdit()
Me.TableAdapterManager.UpdateAll(Me.OneDataDataSet)
End Sub
Private Sub btnDeleteNote_Click(sender As System.Object, e As System.EventArgs) Handles btnDeleteNote.Click
If MsgBox("Warning, you are about to delete a note, this cannot be undone!", _
CType(vbOKCancel + vbExclamation, MsgBoxStyle), "Warning") = vbOK Then
'Dim row As OneDataDataSet.BidLogRow
Dim RI As String
row = CType(CType(Me.BidLogBindingSource.Current, DataRowView).Row, OneDataDataSet.BidLogRow)
RI = CStr(row.BidLogID)
' Locate the row to delete.
Dim oldRow As OneDataDataSet.BidLogRow
oldRow = OneDataDataSet.BidLog.FindByBidLogID(CInt(RI))
' Delete the row from the dataset
oldRow.Delete()
' Delete the row from the database
Me.BidLogTableAdapter.Update(Me.OneDataDataSet.BidLog)
End If
End Sub
Private Sub btnOk_Click(sender As System.Object, e As System.EventArgs) Handles btnOk.Click
SaveNote()
Me.Close()
End Sub
Private Sub btnSaveNote_Click(sender As System.Object, e As System.EventArgs) Handles btnSaveNote.Click
If Me.txtLogNote.Text > Nothing Then
SaveNote()
End If
End Sub
Private Sub txtLogNote_ModifiedChanged(sender As Object, e As System.EventArgs) Handles txtLogNote.ModifiedChanged
btnSaveNote.Enabled = True
btnOk.Enabled = True
End Sub
Dim newLogRow As OneDataDataSet.BidLogRow
Private Sub NewNote()
Dim staffid = CInt(cmbStaff.SelectedValue)
If txtLogNote.Text > Nothing Then
newLogRow = OneDataDataSet.BidLog.NewBidLogRow
newLogRow.BidID = ProjectID
newLogRow.BidLog = txtLogNote.Text
newLogRow.BidLogged = dtpickLogged.Value
newLogRow.StaffID = staffid
OneDataDataSet.BidLog.Rows.Add(newLogRow)
End If
End Sub
Private Sub dtpickLogged_ValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles dtpickLogged.ValueChanged
btnSaveNote.Enabled = True
End Sub
Private Sub txtLogNote_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtLogNote.TextChanged
End Sub
End Class
I hope someone can help.