• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 398
  • Last Modified:

Add Columns and Rows to Existing Dataset

Hello,

I already have dataset and want to add 3 more columns and insert data into it. How can I do it in VB.NET.
0
ASPDEV
Asked:
ASPDEV
1 Solution
 
Alfred A.Commented:
Hi,

I suppose you are really referring to a DataTable.  You can try the following:

Adding a Row example:

Dim dRow as DataRow = Dataset.Tables("NameOfDataTable").NewRow()
 dRow("columnName1"] = "columnValue1"
 dRow("columnName2"] = "columnValue2"
 Dataset.Tables("NameOfDataTable").Rows.Add(dRow)

Adding a Column example:

Dim dCol1 As DataColumn = New DataColumn("OrderID", System.Type.GetType("System.Int32"))
Dim dCol2 As DataColumn = New DataColumn("Description", System.Type.GetType("System.String"))
Dim dCol3 As DataColumn = New DataColumn("OrderName", System.Type.GetType("System.String"))

Dataset.Tables("NameOfDataTable").Columns.Add(dCol1)
Dataset.Tables("NameOfDataTable").Columns.Add(dCol2)
Dataset.Tables("NameOfDataTable").Columns.Add(dCol3)

I hope this helps.
0
 
ASPDEVAuthor Commented:
No, I need if possible to add Columns, Rows to an existing Dataset without having any schema issue.

My already existing Dataset has

Id       FName   LName

1       John     David

2        Peter     Troy


I tried Ds.Merge() method, it doesn't work either.
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
Alfred A.Commented:
Hi ASPDEV,

Have you tried using ds.Clone() method?  You can try the following:

1.  Clone an existing Dataset
2.  Copy the DataRows, existing and to be added, using ImportRow
3.  You can then overwrite the existing one with the new Dataset.
0
 
ASPDEVAuthor Commented:
How can add two datatables to a single dataset.

0
 
Alfred A.Commented:
Hi ASPDEV,

Check the following website:

http://msdn.microsoft.com/en-us/library/kax0zhe7%28VS.80%29.aspx

It explains about ds.Copy() and ds.Clone().
0
 
Alfred A.Commented:
Hi ASPDEV,

To join the tables, you can use the GetParentRow() method in combination with a "Relation" in the DataSet.
0
 
Alfred A.Commented:
Hi,

Or you can do the something like this, there might be some typos or missing parts but I just quickly generated one.

Dimt ds as Dataset = New DataSet()
ds.Tables.Add(Table1)
ds.Tables.Add(Table2)
ds.Relations.Add("JoinName", Table1.Columns("Test1"), Table2.Columns("Test1"))
Table2.Columns.Add("Test2", System.Type.GetType("System.String"), "Parent.Test2")
0
 
CodeCruiserCommented:
Did you try something like this?

'dSet is a dataset

With dSet
    .Tables(0).Columns.Add("Address", GetType(String))

    For i as Integer = 0 to .Tables(0).Rows.Count - 1
         .Tables(0).Rows(i).Item(3) = "somevalue"
    Next
End With
0
 
ASPDEVAuthor Commented:
Thanks
0

Featured Post

2018 Annual Membership Survey

Here at Experts Exchange, we strive to give members the best experience. Help us improve the site by taking this survey today! (Bonus: Be entered to win a great tech prize for participating!)

Tackle projects and never again get stuck behind a technical roadblock.
Join Now