locked
updating listview and database

    Question

  • I have the following code that adds items into  a listview and groups them into categories, when a row is selected in the listview  the columns are put into 2-textboxes and a combobox.  Now how do I get the changes from the textboxes and combobox back into the listview and  into the database.

    Thanks

    Public Class Form1

    Private oldCode As String

    Private oldName As String

    Private oldCat As String

    Private Sub CostItemsBindingNavigatorSaveItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

    Me.Validate()

    Me.CostItemsBindingSource.EndEdit()

    Me.TableAdapterManager.UpdateAll(Me.EstimatorDataSet)

    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    'TODO: This line of code loads data into the 'EstimatorDataSet.CostItems' table. You can move, or remove it, as needed.

    Me.CostItemsTableAdapter.Fill(Me.EstimatorDataSet.CostItems)

    GroupBox1.Visible = False

    GroupBox2.Visible = False

     

    End Sub

    Private Sub ListView1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListView1.Click

    Dim li As ListViewItem

    li = ListView1.FocusedItem

    Dim selectedCostItemRow As EstimatorDataSet.CostItemsRow

    selectedCostItemRow = EstimatorDataSet.CostItems.FindById(Integer.Parse(li.Tag))

    GroupBox2.Enabled = True

    With TextBox1

    .Text = selectedCostItemRow.Code

    .BackColor = Color.LightYellow

    End With

    With TextBox2

    .Text = selectedCostItemRow.Name

    .BackColor = Color.LightYellow

    End With

    With ComboBox1

    .Text = selectedCostItemRow.SubCategory

    .BackColor = Color.LightYellow

    End With

    oldCode = selectedCostItemRow.Code

    oldName = selectedCostItemRow.Name

    oldCat = selectedCostItemRow.SubCategory

    TextBox1.Focus()

    End Sub

    Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click

    TextBox1.Text = oldCode

    TextBox2.Text = oldName

    ComboBox1.Text = oldCat

    End Sub

    Private Sub EditCostItemToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles EditCostItemToolStripMenuItem.Click

    GroupBox1.Visible = True

    GroupBox2.Visible = True

    GroupBox2.Enabled = False

    ListView1.Columns.Add("Cost Code", 50, HorizontalAlignment.Left)

    ListView1.Columns.Add("Cost Name", 200, HorizontalAlignment.Left)

    ListView1.Groups.Add("Overhead Costs", "Overhead Costs")

    ListView1.Groups.Add("Hard Costs", "Hard Costs")

    ListView1.Groups.Add("Builder Costs", "Builder Costs")

    ListView1.View = View.Details

    ListView1.AllowColumnReorder = True

    ListView1.FullRowSelect = True

    Me.CostItemsTableAdapter.Fill(Me.EstimatorDataSet.CostItems)

    ListView1.Items.Clear()

    ListView1.BeginUpdate()

    Dim costitems As EstimatorDataSet.CostItemsRow

    For Each costitems In EstimatorDataSet.CostItems.Rows

    Dim costitemListItem As New ListViewItem

    costitemListItem.Text = costitems.Code

    costitemListItem.Tag = costitems.Id

    costitemListItem.SubItems.Add(costitems.Name)

    costitemListItem.Group = ListView1.Groups(costitems.SubCategory)

    ListView1.Items.Add(costitemListItem)

    Next

    ListView1.EndUpdate()

    End Sub

    Private Sub AddNewCostItemToolStripMenuItem_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles AddNewCostItemToolStripMenuItem.Click

    GroupBox1.Visible = True

    With TextBox1

    .BackColor = Color.Gray

    .Enabled = False

    End With

    With TextBox2

    .BackColor = Color.Gray

    .Enabled = False

    End With

    With ComboBox1

    .BackColor = Color.Gray

    .Enabled = False

    End With

    ListView1.Columns.Add("", 50, HorizontalAlignment.Left)

    ListView1.Columns.Add("", 200, HorizontalAlignment.Left)

    ListView1.Groups.Add("Overhead Costs", "Overhead Costs")

    ListView1.Groups.Add("Hard Costs", "Hard Costs")

    ListView1.Groups.Add("Builder Costs", "Builder Costs")

    ListView1.View = View.Details

    ListView1.AllowColumnReorder = True

    ListView1.FullRowSelect = False

    Me.CostItemsTableAdapter.Fill(Me.EstimatorDataSet.CostItems)

    ListView1.Items.Clear()

    ListView1.BeginUpdate()

    Dim costitems As EstimatorDataSet.CostItemsRow

    For Each costitems In EstimatorDataSet.CostItems.Rows

    Dim costitemListItem As New ListViewItem

    costitemListItem.Text = costitems.Code

    costitemListItem.Tag = costitems.Id

    costitemListItem.SubItems.Add(costitems.Name)

    costitemListItem.Group = ListView1.Groups(costitems.SubCategory)

    ListView1.Items.Add(costitemListItem)

    Next

    ListView1.EndUpdate()

    End Sub

    End Class

    Monday, September 29, 2008 2:24 PM

Answers

  • Why do you not use the Datagridview control to view and edit the data? If you use it, after you modify the data, call Dataadapter.update method with the datatable(or dataset parameter) to save the data change into the database.

    If you insist to using the listview control, you need to know which item value is displayed and edited in the textbox and combobox, then change the item value based on the modified value. About how to save the data change to database, you can directly issue SQL statement with the SqlCommand(or Oledbcommand) object or if there is the dataset(or datatable object) and you also modify the datatable(or dataset) object, then call dataadapter.update method to save the data into the database.

    Wednesday, October 01, 2008 7:18 AM

All replies

  • Why do you not use the Datagridview control to view and edit the data? If you use it, after you modify the data, call Dataadapter.update method with the datatable(or dataset parameter) to save the data change into the database.

    If you insist to using the listview control, you need to know which item value is displayed and edited in the textbox and combobox, then change the item value based on the modified value. About how to save the data change to database, you can directly issue SQL statement with the SqlCommand(or Oledbcommand) object or if there is the dataset(or datatable object) and you also modify the datatable(or dataset) object, then call dataadapter.update method to save the data into the database.

    Wednesday, October 01, 2008 7:18 AM
  • Thanks for your help on my other thread.  With the help file I was able to figure it out.

     

    Wednesday, October 01, 2008 12:02 PM
  • Hi Randy0128,

    I am glad to know that you got it. I just give some basic advice. You make your effort to figure out it. If you have any further development issues with VB.NET, feel free to post here to discuss it with us. Here are many wonderful community members to share their knowledge and experience to help us in MSDN forum. We really appreciate their helps.

    Best regards,
    Riquel
    Thursday, October 02, 2008 3:17 AM