14

I will have to create columns in datatable during runtime and assign values to it. How can i do it in vb.net. Any sample please...

madth3
  • 7,275
  • 12
  • 50
  • 74
Anuya
  • 8,082
  • 49
  • 137
  • 222

2 Answers2

26

If you want to create dynamically/runtime data table in VB.Net then you should follow these steps as mentioned below :

  • Create Data table object.
  • Add columns into that data table object.
  • Add Rows with values into the object.

For eg.

Dim dt As New DataTable

dt.Columns.Add("Id", GetType(Integer))
dt.Columns.Add("FirstName", GetType(String))
dt.Columns.Add("LastName", GetType(String))

dt.Rows.Add(1, "Test", "data")
dt.Rows.Add(15, "Robert", "Wich")
dt.Rows.Add(18, "Merry", "Cylon")
dt.Rows.Add(30, "Tim", "Burst")
KyleMit
  • 30,350
  • 66
  • 462
  • 664
RKK
  • 367
  • 1
  • 5
  • 10
  • If i dont know how many columns i will have to create, how can you fix the columns like dt.Rows.Add(1, "Test", "data") ??? there can be even 10 columns. How ? Thnx – Anuya Jun 28 '12 at 08:08
2

What have you tried, what was the problem?

Creating DataColumns and add values to a DataTable is straight forward:

Dim dt = New DataTable()
Dim dcID = New DataColumn("ID", GetType(Int32))
Dim dcName = New DataColumn("Name", GetType(String))
dt.Columns.Add(dcID)
dt.Columns.Add(dcName)
For i = 1 To 1000
    dt.Rows.Add(i, "Row #" & i)
Next

Edit:

If you want to read a xml file and load a DataTable from it, you can use DataTable.ReadXml.

Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
  • @Anuya: Those columns **are** dynamic (`New DataColumn....`). If that isn't helful you need to provide more informations what you're trying to achieve. – Tim Schmelter Jun 28 '12 at 08:14
  • I have a XML. when i read each node of XML, i have to create a column in data table. Where Datatable column name = NodeName and Datatable Column value = Node value. Since the No. of nodes in XNL can vary each time, i have to make it such a way that creating of columns and assigning values to datatable should be generic – Anuya Jun 28 '12 at 08:18
  • @Anuya: Then use [DataTable.ReadXml](http://msdn.microsoft.com/en-us/library/system.data.datatable.readxml.aspx). – Tim Schmelter Jun 28 '12 at 08:35
  • Reading an XML is not my problem. My Question is different. I cant do as you advised because the XML will have lot of sub nodes and Datatab;e.ReadXML will not get a section out of entire XML. – Anuya Jun 28 '12 at 08:35
  • @Anuya: Have you tried [`DataSet.ReadXml`](http://msdn.microsoft.com/en-us/library/804ky201) instead which should create DataTables for every sub-node? – Tim Schmelter Jun 28 '12 at 08:45
  • Even that could not be done... Error : The table (add) cannot be the child table to itself in nested relations. – Anuya Jun 28 '12 at 08:51
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/13147/discussion-between-anuya-and-tim-schmelter) – Anuya Jun 28 '12 at 09:05
  • @TimSchmelter, Hey Tim, I was wondering if there was any reason why you wouldn't call the [`Add(String, Type)`](http://msdn.microsoft.com/en-us/library/730zyedy(v=vs.110).aspx) overload instead of taking two steps to first delcare the column and then add it with [`Add(DataColumn)`](http://msdn.microsoft.com/en-us/library/55b10992(v=vs.110).aspx)? I've been trying to move our code base from the later to the former, but you seem like a smart chap, so I was wondering if there was something I was missing. – KyleMit Apr 28 '14 at 17:39
  • @kyle: no, there is no difference apart from the fact that you can access all properties if you have the DataColumn. Since the question was about columns i thought it would be better to initialize and add them instead of using the string/type overload which does that implicitly. – Tim Schmelter Apr 28 '14 at 19:09