Results 1 to 3 of 3

Thread: Add to Dataset in Datagrid????

  1. #1

    Thread Starter
    Fanatic Member venerable bede's Avatar
    Join Date
    Sep 2002
    Location
    The mystic land of Geordies
    Posts
    1,018

    Add to Dataset in Datagrid????

    Using the form wizard I have created a form which
    displays 1 record at a time. The data source is dataset1
    (items in stock).

    I have also added a datagrid to the form but as yet it
    has no datasource.

    The idea is simple.
    The user finds the stock item they wish to add to the
    datagrid and click a button. When the button is clicked
    some of the fields
    e.g. "txtItemDescription", "txtQuantityinstock" are
    copied into a dataset which is displayed in the
    datagrid. The user can do this as many times as they
    like.

    I have to admit to being baffled by this. In Access I
    used to do this in my sleep but being new to .Net I am
    like a child.

    Can anyone point me in the right direction please.

    Thanks All for your help.

    Parksie

  2. #2
    Fanatic Member
    Join Date
    Sep 2002
    Posts
    518
    A number of examples of how to manipulate DataGrids and their bindings from code is at:

    ms-help://MS.VSCC/MS.MSDNVS/cpref/html/frlrfsystemwindowsformsdatagridclassdatasourcetopic.htm

    Hope this helps get you started.

  3. #3
    New Member
    Join Date
    Aug 2002
    Location
    Johannesburg, South Africa
    Posts
    12

    Unhappy

    I am trying to do the exact same thing in asp.net

    Public Sub ConstructTable()
    dsSelection = New DataSet("DataSelect")
    tblSelection = New DataTable("Selection")
    dsSelection.Tables.Add(tblSelection)
    col1 = New DataColumn("DocumentType")
    col1.DataType = System.Type.GetType("System.String")
    col2 = New DataColumn("Key")
    col2.DataType = System.Type.GetType("System.String")
    col3 = New DataColumn("Filter")
    col3.DataType = System.Type.GetType("System.String")
    col4 = New DataColumn("Value1")
    col4.DataType = System.Type.GetType("System.String")
    col5 = New DataColumn("Value2")
    col5.DataType = System.Type.GetType("System.String")
    With tblSelection.Columns
    .Add(col1)
    .Add(col2)
    .Add(col3)
    .Add(col4)
    .Add(col5)
    End With
    End Sub
    Private Sub addToTable()
    Dim drDataRow As DataRow
    drDataRow = dsSelection.Tables("Selection").NewRow
    dsSelection.Tables("Selection").Rows.Add(drDataRow)
    drDataRow("DocumentType") = Me.lblDocumentType.Text
    drDataRow("Key") = Me.txtKey.Text
    drDataRow("Filter") = Me.DropDownList1.SelectedItem.Text
    drDataRow("Value1") = Me.txtSearch1.Text
    drDataRow("Value2") = Me.txtSearch2.Text
    dsSelection.AcceptChanges()
    With Me.dgSelection
    '.PageSize = 10
    .DataSource = New DataView(dsSelection.Tables("Selection"))
    .DataBind()
    End With



    The problem is I am only getting the current data on the grid.


    so I figured something like this might help

    Private Sub addToTable()
    Dim drDataRow As DataRow
    Dim i As Integer = 0
    While i < dsSelection.Tables("Selection").Rows.Count
    drDataRow = dsSelection.Tables("Selection").NewRow
    dsSelection.Tables("Selection").Rows.Add(drDataRow)
    drDataRow("DocumentType") = dsSelection.Tables("Selection").Rows(i).Item("DocumentType")
    drDataRow("Key") = dsSelection.Tables("Selection").Rows(i).Item("Key")
    drDataRow("Filter") = dsSelection.Tables("Selection").Rows(i).Item("Filter")
    drDataRow("Value1") = dsSelection.Tables("Selection").Rows(i).Item("Value1")
    drDataRow("Value2") = dsSelection.Tables("Selection").Rows(i).Item("Value2")
    dsSelection.AcceptChanges()
    With Me.dgSelection
    .DataSource = New DataView(dsSelection.Tables("Selection"))
    .DataBind()
    End With
    i = i + 1
    End While
    drDataRow = dsSelection.Tables("Selection").NewRow
    dsSelection.Tables("Selection").Rows.Add(drDataRow)
    drDataRow("DocumentType") = Me.lblDocumentType.Text
    drDataRow("Key") = Me.txtKey.Text
    drDataRow("Filter") = Me.DropDownList1.SelectedItem.Text
    drDataRow("Value1") = Me.txtSearch1.Text
    drDataRow("Value2") = Me.txtSearch2.Text
    dsSelection.AcceptChanges()
    With Me.dgSelection
    '.PageSize = 10
    .DataSource = New DataView(dsSelection.Tables("Selection"))
    .DataBind()
    End With
    End Sub


    problem is dsSelection.Tables("Selection").Rows.Count always returns 0

    if I ConstructTable on button_click and "Object reference not set to an instance of an object." if not???


    Please!!!! Can sombody show me where I'm Going Wrong.
    Hans

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