vb.net insert into database from checked list box - vb.net

I have a checked list box with 6 items . i use the designer to set the binding source to the coulm in my data base named "Gad" the problem is when i select 2 or 3 items from checked list the coulm "gad" on my database get the first selected only .. i want the all selected items saved in this this coulm separated by dashes like item1 - item2 - item 3

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim bob As New System.Text.StringBuilder
For Each item As String In CheckedListBox1.CheckedItems
bob.Append(item)
bob.Append(" - ")
Next
MessageBox.Show(bob.ToString)
End Sub

Related

VB.NET sharing information between two datagridview

I have two DataGridViews, DataGridView1 is holding information from database, once a user click on an item in the DataGridView1 is should be sent to the DataGridView2, my question is how to specify which information is needed from DataGridView1.
for example DataGridView1 holds thees information :
- ItemName
- ItemDescription
- ItemPrice
in DataGridView2 it has these columns
- Qty (Quantity that is inserted by user)
- ItemName
- ItemPrice
thanks in advance :)
To get the data from the clicked cell/row you can use the cellClick- or rowEnter-event. This example code adds the values 'name' and 'description' from the clicked DGV1-row to the first DGV2-row:
Private Sub DGV1_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles DGV1.CellContentClick
DGV2.rows(e.RowIndex).cells(0).value = DGV2.rows(0).cells(1).value 'itemName
DGV2.rows(e.RowIndex).cells(2).value = DGV2.rows(0).cells(2).value 'itemDescription
End Sub
If you want to constantly add a new row to DGV2 you can use the .rowCount-method of the DGV instead of an specific index for the row.

Windows Forms App Basics: Deleting a row from a bound DataGridView

I have a DataGridView control on a WinForms app that is bound from one of the tables in my database. I would like this DataGridView to change whenever the user "Pulls" a row from the DataGridView to a separate DataGridView located next to it (that is initially blank and not bound to anything).
I'm sure you have seen something like this before, let me draw a diagram. This would be an example of someone who has selected Row1 in the left DataGridView table, and then hit the Pull Button to pull it to the right DataGridView table:
Database: ToBeDeleted:
Row1 ______ Row1
Row2 |Pull|
Row3 |--->| ______________
etc.. ------ |Remove Records|
Now, when the user "Pulls" over one of the records in the left table to the right table, I would like to left table to update itself to not have that Row in it anymore. This way, the user cannot add two of the same row to the righthand table, and it instantly reflects the changes that they are about to make (since the righthand table represents records that are to be deleted from the lefthand table).
I see nothing wrong about your project and it's not difficult to do.
I'm using Linq To Entities for this sample so I have my database table mapped as objects.
Create 2 databound DGVs, bind them to 2 difference datasources (BindingSource) and choose Object. The object type is the same because you're binding them to the same database table (in my case the row object is called as its table, Table3)
Left DGV is bound to Table3BindingSource, right DGV to Table3BindingSourceTBD
Get data for the left DGV from database (a basic select from table)
The button Pull remove an object from left dgv and adds it to the other
The button Delete just loop thru the right rows, and handle the db delete action
Finally, query your db for updated data (left dgv) and clear the right dgv
sample:
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
GetData()
End Sub
Sub GetData()
Dim db = New StackOverflowEntities
Table3BindingSource.DataSource = db.Table3.OrderBy(Function(x) x.Id).ToList()
End Sub
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Try
Dim x = CType(Table3BindingSource.Current, Table3)
'remove from left dgv
Table3BindingSource.RemoveCurrent()
'add to right dgv
Table3BindingSourceTBD.Add(x)
Catch ex As Exception
End Try
End Sub
Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
'delete rows in datagridview TBD
Dim db = New StackOverflowEntities
'loop rows
For Each r In Table3BindingSourceTBD
'cast right dgv row to underlying object
Dim y = CType(r, Table3)
'set object to be deleted
Dim dr = (From x In db.Table3
Where x.Id = y.Id
Select x).Single()
db.Table3.DeleteObject(dr)
Next
Try
'commit changes to db
db.SaveChanges()
MsgBox("db updated")
Catch ex As Exception
Finally
'refresh all data in left dgv
GetData()
'simply clear right dgv
Table3BindingSourceTBD.Clear()
End Try
End Sub
I used basic Linq To Entities but anything would work (pure SQL, Linq2SQL...). If you need the project source just ask
good luck

I am trying to populate drop boxes

Here is what I have so far to populate a couple drop down boxes in visual basic.
I may need global variables but I am not sure how to do that. I have been told to use Dim and the variable names of the drop boxes but I keep getting errors saying they are already friends within the form.
*This is a homework assignment, however I am not asking you to do the assignment but simply asking help with the problem I am having within the drop boxes. I don't expect you wonderful people to earn my grades for me! Thank you for any help!
Public Class Form1
'// Mortgage Calculator Assignment 1.
'// Can calculate payments on a given loan at a given rate for a given term.
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
'// Inputs rate starting at 3.25% through 6.75% at steps of 0.25% into the drop down box.
Private Sub cbRate_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cbRate.SelectedIndexChanged
For i As Decimal = 3.25 To 6.75 Step 0.25
cbRate.Items.Add(i & "%")
Next
End Sub
'// Inputs term in years from 10 through 40 at steps of 5 year intervals into the drop down box.
Private Sub cbTerm_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cbTerm.SelectedIndexChanged
For i As Integer = 10 To 40 Step 5
cbTerm.Items.Add(i)
Next
End Sub
End Class
You do not need global variables.
Try just populating the combo boxes in the form load:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'// Inputs rate starting at 3.25% through 6.75% at steps of 0.25%
' into the drop down
For i As Decimal = 3.25 To 6.75 Step 0.25
cbRate.Items.Add(i & "%")
Next
'// Inputs term in years from 10 through 40 at steps of 5 year intervals
' into the drop down box.
For i As Integer = 10 To 40 Step 5
cbTerm.Items.Add(i)
Next
End Sub
Edit: the combo boxes were declared (probably) for you when you added them to the form in the designer.

set selected item in combobox - vb.net

I am using this code to add a value to a combobox different then the one displayed: how to add value to combobox item
Lets suppose i have 3 values in my Combobox:
item 1
item 2
item 3
If i chose item 2 from the dropdown the code in the link works. But if i TYPE item 2 manually it doesnt work because i think that typing it only sets the combobox1.text value and not the combobox1.selecteditem. I can type a value present in the dropdown, or one not present. If i type one that is present, then the selectedItem property should also be set to the proper value. can this be done?
Thanks
solved this way:
Private Sub ComboBox1_Keyup(sender As Object,
e As System.Windows.Forms.KeyEventArgs) Handles ComboBox1.KeyUp
ComboBox1.SelectedIndex = ComboBox1.FindStringExact(ComboBox1.Text)
End Sub

Unable to delete rows from .sdf database after deleting from DataGridView

I have added a .sdf file(sqlceserver v3.5) to my winforms project.Then I added a .sdf database and select the dataset model for it. I also added a datagridview to this and selected its datasource as "LogBindingSource1" from the scroll pane. Rest code was generated automatically. I selected the columns of the datagridview.Now when i run the project and delete the rows from the grid, the changes are simply not reflecting into the database.
I tried doing the following:
Private Sub DataGridView1_UserDeletedRow(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewRowEventArgs) Handles DataGridView1.UserDeletedRow
For i As Integer = 0 To DataGridView1.SelectedRows.Count - 1
'Me.Database1DataSet3.log.Rows.RemoveAt(Convert.ToUInt16(DataGridView1.Rows(DataGridView1.SelectedRows(i).Index).Cells(0).Value))
'Me.LogTableAdapter1.Delete(Convert.ToUInt16(DataGridView1.Rows(DataGridView1.SelectedRows(i).Index).Cells(0).Value))
'Me.DataGridView1.Rows.RemoveAt(Me.DataGridView1.SelectedRows(i).Index)
'Dim ind As Integer = DataGridView1.SelectedRows(i).Index
Me.LogBindingSource1.RemoveAt(e.Row.Index)
DataGridView1.Refresh()
DataGridView1.Update()
'Me.LogTableAdapter1.Update(Me.Database1DataSet3.Tables(0))
Next
End Sub
I am showing the comments to show all the permutations which I tried. It must be noted that Log is the name of the table which has been bound to the grid and is the single table in database.
Before asking, I visited the following resources:
1) https://stackoverflow.com/questions/10575169/inserting-data-from-a-datagridview-to-a-sdf-sql-database
2) https://stackoverflow.com/questions/10575169/inserting-data-from-a-datagridview-to-a-sdf-sql-database (does not use table adapter)
3) http://www.codeproject.com/Articles/24656/A-Detailed-Data-Binding-Tutorial (too verbose)
I just noticed this unanswered question. Here is an easy way to go through your highlighted rows and delete from the datagridview and the database. You will need to provide your own connection string and database name. Here, I call it Ranchconn.
Private Sub btnDelete_Click(sender As Object, e As EventArgs) Handles btnDelete.Click
Ranchconn.Open()
cmdRanch.Connection = Ranchconn
For Each row As DataGridViewRow In cdgvRanchEntry.SelectedRows
cmdRanch.CommandText = "Delete * FROM Entries where ID = " & row.Cells("ID").Value
cmdRanch.ExecuteNonQuery()
cdgvRanchEntry.Rows.Remove(row)
Next row
Ranchconn.Close()
End Sub