DataGridView deleting row save - vb.net

my problem is that i have datagridview, and i select row i click delete button but when i refresh it is back
Private Sub btnDlt_Click(sender As Object, e As EventArgs) Handles btnDlt.Click
For Each row As DataGridViewRow In dgwData.SelectedRows
dgwData.Rows.Remove(row)
SQL.DBDA.Update(SQL.DBDT)
Next
End Sub
and my upadte is working good, it is saving everything, here is code
Private Sub btnUpdate_Click(sender As Object, e As EventArgs) Handles btnUpdate.Click
If MsgBox("Nazaj si prajete uložiť tieto zmeny?", MsgBoxStyle.YesNo) = MsgBoxResult.Yes Then
SQL.DBDA.Update(SQL.DBDT)
loadGrid()
btnUpdate.Enabled = False
If SQL.hasException() Then
Exit Sub
End If
Else
btnUpdate.Enabled = False
loadGrid()
End If
End Sub

The problem should be with your datasource, check your delete query if it work fine.

Related

notification popup when new data is added to the DataGridview vb.net

I am building a standalone application with MS Access as the database. When I update the data from the application, I want the data in the DataGridView to also be updated on all of the other computers that are running the application. For that I have LoadData() but when a new ROW is added I am not getting the notification popup.
I really appreciate any help. Thanks in advance. I am not a pro in coding.
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
LoadData()
End Sub
Private Sub dgvNotify_RowsAdded(sender As Object, e As DataGridViewRowsAddedEventArgs) Handles dgvNotify.RowsAdded
Dim targetRow = Me.dgvNotify.Rows.Cast(Of DataGridViewRow)().OrderByDescending(Function(r) r.Cells("ID").Value).FirstOrDefault
If targetRow IsNot Nothing Then
targetRow.Selected = True
New_Notification()
End If
End Sub
Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing
Me.WindowState = FormWindowState.Minimized
Me.Visible = False
e.Cancel = True
End Sub
Private Sub New_Notification()
NotifyIcon1.BalloonTipTitle = "New Important Notification"
NotifyIcon1.BalloonTipText = "Open the notification application to check the notifications" & vbCrLf & "If you have any question reachout to ops leads:)"
NotifyIcon1.ShowBalloonTip(3000)
End Sub

CheckBox Data to ListBox (VBNet)

the problem with my code is that when i initially select a particular checkbox it works well and displays selected item in listbox, but when selected again it creates another entry within the listbox and when i remove it i have to uncheck the same checkbox the times it has been displayed in the listbox when i click the btnSubmit.
Can someone tell me what's wrong with the code, thank you so much
Private Sub btnSubmit_Click(sender As Object, e As EventArgs) Handles btnSubmit.Click
If CheckBox1.Checked Then
ListBox1.Items.Add(CheckBox1.Text)
Else
ListBox1.Items.Remove(CheckBox1.Text)
End If
If CheckBox2.Checked Then
ListBox1.Items.Add(CheckBox2.Text)
Else
ListBox1.Items.Remove(CheckBox2.Text)
End If
If CheckBox3.Checked Then
ListBox1.Items.Add(CheckBox3.Text)
Else
ListBox1.Items.Remove(CheckBox3.Text)
End If
If CheckBox4.Checked Then
ListBox1.Items.Add(CheckBox4.Text)
Else
ListBox1.Items.Remove(CheckBox4.Text)
End If
If CheckBox5.Checked Then
ListBox1.Items.Add(CheckBox5.Text)
Else
ListBox1.Items.Remove(CheckBox5.Text)
End If
If CheckBox6.Checked Then
ListBox1.Items.Add(CheckBox6.Text)
Else
ListBox1.Items.Remove(CheckBox6.Text)
End If
End Sub
You can do it like this:
Private Sub btnSubmit_Click(sender As Object, e As EventArgs) Handles btnSubmit.Click
ListBox1.Items.Clear
CheckObj(CheckBox1)
CheckObj(CheckBox2)
CheckObj(CheckBox3)
CheckObj(CheckBox4)
CheckObj(CheckBox5)
CheckObj(CheckBox6)
End Sub
Sub CheckObj (obj as Checkbox)
if obj.checked then ListBox1.Items.Add (obj.text)
End Sub

How to Drag And Drop selected rows between DataGridViews

I need to implement drag and drop of the selected rows from one grid to the other:
my code:
datagridview1
Private Sub datagridview1_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles datagridview1.MouseDown
Dim info As DataGridView.HitTestInfo = Me.datagridview1.HitTest(e.X, e.Y)
If info.Type = DataGridViewHitTestType.Cell And e.Button = Windows.Forms.MouseButtons.Left Then
Me.datagridview1.DoDragDrop(datagridview1.SelectedRows.Cast(Of DataGridViewRow).OrderBy(Function(r) r.Index).ToArray, DragDropEffects.All)
End If
End Sub
datagridview2
Private Sub datagridview2_DragEnter(sender As Object, e As DragEventArgs) Handles datagridview2.DragEnter
e.Effect = DragDropEffects.All
End Sub
Private Sub datagridview2_DragDrop(sender As Object, e As DragEventArgs) Handles datagridview2.DragDrop
Try
Dim Rows() As DataGridViewRow = DirectCast(e.Data.GetData(GetType(DataGridViewRow())), DataGridViewRow())
For Each row As DataGridViewRow In rows
MsgBox(row.Cells("ID").Value)
Next
Catch ex As Exception
End Try
End Sub
but there is an error in conversion.
any ideas?
Try sending a copy of the selected rows instead:
Dim dgrCopy(datagridview1.SelectedRows.Count - 1) As DataGridViewRow
datagridview1.SelectedRows.CopyTo(dgrCopy, 0)
Me.datagridview1.DoDragDrop(dgrCopy.OrderBy(Function(r) r.Index).ToArray, DragDropEffects.All)

Close Form1 If FileExists+ Open Form2

I try to close or hide the Form cnx if a file exist and open the Form Product.
But Something is wrong and i don't understand why this dont work.
Private Sub cnx_Load(sender As Object, e As EventArgs) Handles MyBase.Load
strFileName = "app.txt"
strBasePath = Application.StartupPath
If My.Computer.FileSystem.FileExists(strFileName) = True Then
Product.Show()
Me.Hide()
ElseIf My.Computer.FileSystem.FileExists(strFileName) = False Then
MessageBox.Show("File App.config is Missing! Create a new Database.",
"Something is Wrong!", MessageBoxButtons.OK, MessageBoxIcon.Warning)
End If
End Sub
Thanks.
You could do something like this, although probably not optimal.
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
hideForm(Me)
Form2.Show()
End Sub
Private Sub hideForm(form As Form)
form.Opacity = 0.0F
form.ShowInTaskbar = False
End Sub
Also remember to add under Form2 or your program will stay open after closing Form2.
Private Sub Form2_FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing
Form1.Close()
End Sub

Changes To Data Set Not Saving

When I add a new row to my data set it is visible on that specific form in the datagridview, however when I switch to another form with the same data bound datagridview the new row is not there. When I close the program my new row is completely gone then. I want to to save the new row to the Access database it is reading from.
Public Class frmAddStudent
Private Sub btnBack_Click(sender As Object, e As EventArgs) Handles btnBack.Click
Me.Hide()
frmUserControls.Show()
End Sub
Private Sub frmAddStudent_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'TODO: This line of code loads data into the 'StudentRecords1DataSet.tblLecturer' table. You can move, or remove it, as needed.
Me.TblLecturerTableAdapter.Fill(Me.StudentRecords1DataSet.tblLecturer)
'TODO: This line of code loads data into the 'StudentRecords1DataSet.tblStudents' table. You can move, or remove it, as needed.
Me.TblStudentsTableAdapter.Fill(Me.StudentRecords1DataSet.tblStudents)
End Sub
Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click
Dim MyNewRow As DataRow
MyNewRow = StudentRecords1DataSet.tblStudents.NewRow
Try
With MyNewRow
.Item(1) = txtID.Text
.Item(2) = txtFirstName.Text
.Item(3) = txtSurname.Text
End With
Me.Validate()
Me.TblStudentsBindingSource.EndEdit() 'Change this to your binding source ' eg table'
Me.TableAdapterManager.UpdateAll(Me.StudentRecords1DataSet) ' chnage this to your database name'
MessageBox.Show("The Data Has Been Saved", "Information", MessageBoxButtons.OK)
Catch ex As Exception
'if there is a problem saving the data, it will show a messagebox with the problem as to why it could'nt save the data'
MessageBox.Show(ex.Message)
End Try
StudentRecords1DataSet.tblStudents.Rows.Add(MyNewRow)
StudentRecords1DataSet.tblStudents.AcceptChanges()
End Sub
Private Sub txtFirstName_MaskInputRejected(sender As Object, e As MaskInputRejectedEventArgs)
End Sub
Private Sub TblStudentsBindingNavigator_RefreshItems(sender As Object, e As EventArgs) Handles TblStudentsBindingNavigator.RefreshItems
End Sub
End Class
Any suggestions please?
UPDATe X2 based off your question update.
try this,
Public Class frmAddStudent
Private Sub btnBack_Click(sender As Object, e As EventArgs) Handles btnBack.Click
Me.Hide()
frmUserControls.Show()
End Sub
Private Sub frmAddStudent_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'TODO: This line of code loads data into the 'StudentRecords1DataSet.tblLecturer' table. You can move, or remove it, as needed.
Me.TblLecturerTableAdapter.Fill(Me.StudentRecords1DataSet.tblLecturer)
'TODO: This line of code loads data into the 'StudentRecords1DataSet.tblStudents' table. You can move, or remove it, as needed.
Me.TblStudentsTableAdapter.Fill(Me.StudentRecords1DataSet.tblStudents)
End Sub
Private Async Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click
Try
Me.Validate()
Me.TblStudentsBindingSource.EndEdit() 'Change this to your binding source ' eg table'
Me.TableAdapterManager.UpdateAll(Me.StudentRecords1DataSet) ' chnage this to your database name'
MessageBox.Show("The Data Has Been Saved", "Information", MessageBoxButtons.OK)
Await Task.Delay(100)
TblStudentsBindingNavigator_RefreshItems.studentsBindingSource.AddNew()
Catch ex As Exception
'if there is a problem saving the data, it will show a messagebox with the problem as to why it could'nt save the data'
MessageBox.Show(ex.Message)
End Try
End Sub
Private Sub txtFirstName_MaskInputRejected(sender As Object, e As MaskInputRejectedEventArgs)
End Sub
Private Sub TblStudentsBindingNavigator_RefreshItems(sender As Object, e As EventArgs) Handles TblStudentsBindingNavigator.RefreshItems
End Sub
End Class