vb.net : Set XML as Datasource for DataGrid

To set an XML document as a data source in a DataGridView control in VB.NET WinForms, you can use the following steps:

Load the XML data into a DataSet.

Dim ds As New DataSet()
ds.ReadXml(xmlFilePath)

Set the DataSet as the DataSource for the DataGridView control.
DataGridView1.DataSource = ds.Tables(0)

DataGridView1.DataSource = ds.Tables(0)

Optionally, you can customize the appearance of the DataGridView control by setting properties such as ColumnHeadersVisible, SelectionMode, etc.

DataGridView1.ColumnHeadersVisible = True
DataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect

To load an XML string into a DataSet in VB.NET, you can use the following code:

Dim ds As New DataSet()
Dim xmlString As String = “” & _
” & _
Nava” & _
FP0100001” & _
2022-12-17” & _
” & _
” & _

Dim sr As New StringReader(xmlString)
ds.ReadXml(sr)

The StringReader class allows you to read data from a string as if it were a stream of characters. After reading the XML data into a DataSet, you can set it as the data source for a DataGridView control

The error message “Illegal characters in path” usually occurs when the path to a file contains invalid characters.

In this case, it could mean that the path to the XML file contains characters that are not allowed in a file path. For example, the following characters are not allowed in a file path: \ / : * ? ” < > |

To resolve this issue, you can modify the file path to remove the invalid characters. Additionally, you can encode the file path as a URL and then decode it back to a file path to ensure that all characters are valid.

Here’s an example of how to encode and decode a file path in VB.NET:

Dim xmlFilePath As String = “C:\Example\File.xml”
Dim encodedFilePath As String = Uri.EscapeDataString(xmlFilePath)
Dim decodedFilePath As String = Uri.UnescapeDataString(encodedFilePath)

Convert Object To Datatable

Private Sub LoadDataGridView()
Dim recordList As List(Of Record) = GetRecordList() ‘Get a list of Record objects
Dim dt As New DataTable()

‘Add columns to the DataTable
dt.Columns.Add(“POSName”, GetType(String))
dt.Columns.Add(“BillNo”, GetType(String))
dt.Columns.Add(“Date”, GetType(DateTime))
dt.Columns.Add(“Time”, GetType(DateTime))
dt.Columns.Add(“SubTotal”, GetType(Decimal))
dt.Columns.Add(“DiscountAmount”, GetType(Decimal))
dt.Columns.Add(“GrandTotalAmount”, GetType(Decimal))
dt.Columns.Add(“Taxes”, GetType(List(Of Tax)))
dt.Columns.Add(“SettelementMode”, GetType(String))
dt.Columns.Add(“TransactionStatus”, GetType(String))

‘Populate the DataTable with data from the Record objects
For Each rec As Record In recordList
Dim row As DataRow = dt.NewRow()
row(“POSName”) = rec.POSName
row(“BillNo”) = rec.BillNo
row(“Date”) = rec.Date
row(“Time”) = rec.Time
row(“SubTotal”) = rec.SubTotal
row(“DiscountAmount”) = rec.DiscountAmount
row(“GrandTotalAmount”) = rec.GrandTotalAmount
row(“Taxes”) = rec.Taxes
row(“SettelementMode”) = rec.SettelementMode
row(“TransactionStatus”) = rec.TransactionStatus
dt.Rows.Add(row)
Next

‘Set the DataTable as the data source for the DataGridView control
DataGridView1.DataSource = dt
End Sub

Scroll to Top