Results 1 to 6 of 6

Thread: [2005] Data Table, add new row if there is something in the table?

  1. #1

    Thread Starter
    Member
    Join Date
    Jan 2009
    Posts
    36

    [2005] Data Table, add new row if there is something in the table?

    Hi guys, basically i have made the data table in vb code, and it works flawlessly. It gets its values from labels which are part of a calculation.
    It shows the information to the user in a datagridview. Now i would like it to add new rows and fill them in if the user has already entered the data once and it has been logged in the datagridview.

    here is the current code:
    Code:
    'DATA TABLE
            Dim Table1 As DataTable
            Table1 = New DataTable("CF Log")
            'creating a table named CF Log
            Dim Row1, Row2 As DataRow
            'declaring three rows for the table
            Dim ds As New DataSet()
            Try
                Dim Blank As DataColumn = New DataColumn(" ")
                Blank.DataType = System.Type.GetType("System.String")
                Table1.Columns.Add(Blank)
                Dim Dates As DataColumn = New DataColumn("Date")
                Dates.DataType = System.Type.GetType("System.String")
                Table1.Columns.Add(Dates)
                Dim Mileage As DataColumn = New DataColumn("Mileage")
                'declaring a column named Name
                Mileage.DataType = System.Type.GetType("System.String")
                'setting the datatype for the column
                Table1.Columns.Add(Mileage)
                'adding the column to table
                Dim Journey_Length As DataColumn = New DataColumn("Journey Length")
                Journey_Length.DataType = System.Type.GetType("System.String")
                Table1.Columns.Add(Journey_Length)
                Dim Car_Size As DataColumn = New DataColumn("Car Size")
                Car_Size.DataType = System.Type.GetType("System.String")
                Table1.Columns.Add(Car_Size)
                Dim Public_Transport As DataColumn = New DataColumn("Public Transport")
                Public_Transport.DataType = System.Type.GetType("System.String")
                Table1.Columns.Add(Public_Transport)
                Dim flights As DataColumn = New DataColumn("Hours spent on flights")
                flights.DataType = System.Type.GetType("System.String")
                Table1.Columns.Add(flights)
                Dim CF As DataColumn = New DataColumn("Carbon Footprint")
                CF.DataType = System.Type.GetType("System.String")
                Table1.Columns.Add(CF)
            Catch
            End Try
    
            Try
                Table1.NewRow()
                Row1 = Table1.NewRow()
                'declaring a new row
                Row1.Item(" ") = "Selections"
                Row1.Item("Mileage") = Me.TextBox1.Text
                'filling the row with values. Item property is used to set the field value.
                Row1.Item("Journey Length") = Calculations.Label9.Text
                'filling the row with values. 
                Row1.Item("Car Size") = Calculations.Label8.Text
                'filling the row with values. 
                Row1.Item("Public Transport") = Me.TextBox2.Text
                Row1.Item("Hours spent on flights") = Me.TextBox3.Text
                'adding the completed row to the table
                Table1.Rows.Add(Row1)
    
                Row2 = Table1.NewRow()
                Row2.Item(" ") = "Breakdown (Kg)"
                Row2.Item("Date") = Date.Now
                Row2.Item("Mileage") = rounded1
                Row2.Item("Journey Length") = rounded4
                Row2.Item("Car Size") = rounded5
                Row2.Item("Public Transport") = rounded2
                Row2.Item("Hours spent on flights") = rounded3
                Row2.Item("Carbon Footprint") = Output.Label2.Text
                'adding the completed row to the table
                Table1.Rows.Add(Row2)
            Catch
            End Try
    
            ds = New DataSet()
            'creating a dataset
            ds.Tables.Add(Table1)
            'adding the table to dataset 
            Output.DataGridView1.DataSource = ds
            'binding the table to datagrid
            Output.DataGridView1.DataMember = "CF Log"
    
            'save the structure (schema) of this DataSet
            ds.WriteXmlSchema("c:\Logdataset.xml")
            'save the actual data that’s currently in this DataSet
            ds.WriteXml("c:\LogData.xml")

    so how can i get to add 2 new rows before it enter thte data if there is already something in the datagridview?

  2. #2
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: [2005] Data Table, add new row if there is something in the table?

    you just do a row count on the datatable and if the count is > 0, you add new row to it. Something like this:
    Code:
    Dim table as datatable = DirectCast(Output.DataGridView1.DataSource, DataSet).Tables("CF Log")
    If Not table is nothing then
        if table.rows.count > 0 Then
           table.Rows.Add(table.Newrow())
        end if
    end if
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

  3. #3

    Thread Starter
    Member
    Join Date
    Jan 2009
    Posts
    36

    Re: [2005] Data Table, add new row if there is something in the table?

    i tried it like this, but it didnt do anything:

    Code:
            'DATA TABLE
            Dim Table1 As DataTable
            Table1 = New DataTable("CF Log")
            'creating a table named CF Log
            Dim Row1, Row2 As DataRow
            'declaring three rows for the table
            Dim ds As New DataSet()
            ds = New DataSet()
            'creating a dataset
            ds.Tables.Add(Table1)
            'adding the table to dataset 
            Output.DataGridView1.DataSource = ds
            'binding the table to datagrid
            Output.DataGridView1.DataMember = "CF Log"
    
            Dim table As DataTable = DirectCast(Output.DataGridView1.DataSource, DataSet).Tables("CF Log")
            If Not table Is Nothing Then
                If table.Rows.Count > 0 Then
                    table.Rows.Add(Table1.NewRow())
                    table.Rows.Add(Table1.NewRow())
                End If
            End If
            Try
                Dim Blank As DataColumn = New DataColumn(" ")
                Blank.DataType = System.Type.GetType("System.String")
                Table1.Columns.Add(Blank)
                Dim Dates As DataColumn = New DataColumn("Date")
                Dates.DataType = System.Type.GetType("System.String")
                Table1.Columns.Add(Dates)
                Dim Mileage As DataColumn = New DataColumn("Mileage")
                'declaring a column named Name
                Mileage.DataType = System.Type.GetType("System.String")
                'setting the datatype for the column
                Table1.Columns.Add(Mileage)
                'adding the column to table
                Dim Journey_Length As DataColumn = New DataColumn("Journey Length")
                Journey_Length.DataType = System.Type.GetType("System.String")
                Table1.Columns.Add(Journey_Length)
                Dim Car_Size As DataColumn = New DataColumn("Car Size")
                Car_Size.DataType = System.Type.GetType("System.String")
                Table1.Columns.Add(Car_Size)
                Dim Public_Transport As DataColumn = New DataColumn("Public Transport")
                Public_Transport.DataType = System.Type.GetType("System.String")
                Table1.Columns.Add(Public_Transport)
                Dim flights As DataColumn = New DataColumn("Hours spent on flights")
                flights.DataType = System.Type.GetType("System.String")
                Table1.Columns.Add(flights)
                Dim CF As DataColumn = New DataColumn("Carbon Footprint")
                CF.DataType = System.Type.GetType("System.String")
                Table1.Columns.Add(CF)
            Catch
            End Try
    
            
    
            Try
                Table1.NewRow()
                Row1 = Table1.NewRow()
                'declaring a new row
                Row1.Item(" ") = "Selections"
                Row1.Item("Mileage") = Me.TextBox1.Text
                'filling the row with values. Item property is used to set the field value.
                Row1.Item("Journey Length") = Calculations.Label9.Text
                'filling the row with values. 
                Row1.Item("Car Size") = Calculations.Label8.Text
                'filling the row with values. 
                Row1.Item("Public Transport") = Me.TextBox2.Text
                Row1.Item("Hours spent on flights") = Me.TextBox3.Text
                'adding the completed row to the table
                Table1.Rows.Add(Row1)
    
                Row2 = Table1.NewRow()
                Row2.Item(" ") = "Breakdown (Kg)"
                Row2.Item("Date") = Date.Now
                Row2.Item("Mileage") = rounded1
                Row2.Item("Journey Length") = rounded4
                Row2.Item("Car Size") = rounded5
                Row2.Item("Public Transport") = rounded2
                Row2.Item("Hours spent on flights") = rounded3
                Row2.Item("Carbon Footprint") = Output.Label2.Text
                'adding the completed row to the table
                Table1.Rows.Add(Row2)
            Catch
            End Try
    
    
           
            'save the structure (schema) of this DataSet
            ds.WriteXmlSchema("c:\Logdataset.xml")
            'save the actual data that’s currently in this DataSet
            ds.WriteXml("c:\LogData.xml")
    
    
        End Sub

  4. #4

    Thread Starter
    Member
    Join Date
    Jan 2009
    Posts
    36

    Re: [2005] Data Table, add new row if there is something in the table?

    i still cant figure this one out, how can i get it to Add new rows if there is data in the data grid already, then put in the information into the new rows rather than the exising rows??

    anyone got any ideas? the row count seems to only see 0 rows, as it only does a function when it is set to = 0.

    i have moved the binding and creating the dataset and datagridview to the top of the code as well.

    still not getting anywhere

  5. #5

    Thread Starter
    Member
    Join Date
    Jan 2009
    Posts
    36

    Re: [2005] Data Table, add new row if there is something in the table?

    bump, still cant do this, anyone got an idea?

  6. #6

    Thread Starter
    Member
    Join Date
    Jan 2009
    Posts
    36

    Re: [2005] Data Table, add new row if there is something in the table?

    bump

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width