locked
Adding data to a table RRS feed

  • Question

  • I have a datagrid on my form I want to populate with data that is being calculated on my form, and after looking around for awhile I decided to post here. Please keep in mind I am very new to VB and I am just learning. Currently on my form I have Textboxes to display the data that I want added to my table just so I can see that everything is working as it should and it does now if I could just add the information to my table things would be better. If anyone could be so kind and give me a clue how I can add a record with the data that is contained in txtDesc, txtWebHrs, txtWebDollars and txtTotal it would be greatly appreciated. One more quick note how can I control how many decimal places are used for example I would like to round the Price field to only store 2 positions past the decimal.

    Public Class Form1

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

    Dim QTY As Integer = 0

    Dim Pages As Integer = 0

    Dim sig1qty As Integer = 0

    Dim sig1val As Integer = 0

    Dim sig2qty As Integer = 0

    Dim sig2val As Integer = 0

    Dim sig3qty As Integer = 0

    Dim sig3val As Integer = 0

    Dim WebHours As Decimal

    Dim WebDollars As Decimal

    Dim Description As String

    Dim Price As Decimal

    QTY = Integer.Parse(txtQTY.Text)

    Pages = Integer.Parse(txtPages.Text)

    sig1qty = Integer.Parse(txtSig1Qty.Text)

    sig1val = Integer.Parse(txtSig1Val.Text)

    Description = "Web Press"

    txtDesc.Text = Description

    WebHours = sig1qty * QTY / 18000

    txtWebHrs.Text = WebHours

    WebDollars = 78.81

    txtWebDollars.Text = WebDollars

    Price = WebHours * WebDollars

    TxtTotal.Text = Price

    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 'StrangDataSet.TestQuote' table. You can move, or remove it, as needed.

    Me.TestQuoteTableAdapter.Fill(Me.StrangDataSet.TestQuote)

    End Sub

    End Class

    Tuesday, March 28, 2006 9:46 PM

Answers

  • hi,

    in your case you allready have a table so you will not need to creat a new one you will just add to existing table, you can try something like that


    Module Module1
    Dim dt As New DataTable("mytable")
    Sub Main()
    dt.Columns.Add(
    "col1")
    dt.Columns.Add(
    "col2")
    dt.Columns.Add(
    "col3")
    'call a method to add data to table

    addrow("row1", "123", "something")
    addrow(
    "row2", "456", "def")
    addrow(
    "row3", "789", "nml")
    'display table data

    For Each dtr As DataRow In dt.Rows
    Console.WriteLine(dtr(
    "col1") & ", " & dtr("col2"))
    Next

    End Sub

    Private Sub addrow(ByVal var1 As String, ByVal var2 As String, ByVal var3 As String)
    Dim dr As DataRow = dt.NewRow
    dr(
    "col1") = var1
    'formating a double value as currency with just 2 fractions

    'if you don't want currency sign you can replace C2 by f2

    dr("col2") = String.Format("{0:C2}", Double.Parse(var2))
    dr(
    "col3") = var3
    dt.Rows.Add(dr)
    End Sub
    End
    Module


     

    hope this helps

    Tuesday, March 28, 2006 10:11 PM