This is another interesting item I ran into from the field. A customer wants to use XML with a DataGridView, but the issue is that they want to manipulate the look of the data in the datagridview during design time. My solution was to create a strongly-typed dataset and then read the xml into the dataset. I need to give credit to Rong-Chun Zhang on the MSDN Forums for helping me solve this problem. Here is the link to the topic at MSDN - http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=3543590&SiteID=1 The created project is nothing more than a simple Windows Forms Project.
Source XML
Here is the XML file I used (same as the one from the forum topic)
<MyDataSet xmlns="MyDataSetnamespace">
<Order_Details>
<OrderID>10248</OrderID>
<ProductID>11</ProductID>
<UnitPrice>14.0000</UnitPrice>
<Quantity>12</Quantity>
<Discount>0</Discount>
</Order_Details>
<Order_Details>
<OrderID>10249</OrderID>
<ProductID>11</ProductID>
<UnitPrice>14.0000</UnitPrice>
<Quantity>12</Quantity>
<Discount>0</Discount>
</Order_Details>
<Order_Details>
<OrderID>10250</OrderID>
<ProductID>11</ProductID>
<UnitPrice>14.0000</UnitPrice>
<Quantity>12</Quantity>
<Discount>0</Discount>
</Order_Details>
</MyDataSet>
Strongly-Typed Dataset
Next, you need to create a strongly typed dataset. What you need to do is add a new item to your project and then select dataset. Give the dataset a meaningful name and click Add. This will bring you to the dataset designer. From there you can add a datatable and all the associated columns. Here is the result that I had:
I saved the changes and now the dataset shows up in the Data Sources Tab. Now you can add it to your form and manipulate it as you would any other databound datagridview control.
Code Behind
Finally, you need to load the XML into the dataset. The code is as follows:
Try
'You need to set the namespace on the dataset to match the one on the
'xml file.
MyDataSet1.Namespace = "MyDataSetnamespace"
MyDataSet1.ReadXml(txtXMLFileLocation.Text)
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
This post was really simplistic, but I am going to do more research into how to use more complex XML files with the datagridview, assuming there is any interest.
To get things started I figured I would include a few links to Microsoft’s Small Business Developer website. The information is good, though I think it only gives you a taste of what you can do.
Small Business Developer Website
Small Business Developer Forum
Happy Hunting.
Welcome. This is my first attempt at blogging, so bear with me. What I hope to accomplish is to post code and tips for the small business developer. Personally, I love the idea of working with small businesses to fulfil their software needs. They aren’t loaded with money, so as a developer you need to find unique ways to build software that fits their needs and budget. A good example is report writing. Typically, a business will use Excel or Access to display their data. That might be ok now, but there are limitations to what it can do. Coding is done using VBA, which is a bit old and tired. An alternative would be to write any business logic in VB.Net/C# and have it output the data into XML (e.g. SpreadSheetML for Excel) for Office 2003/2007, earlier versions will need to use Office Automation. From there the user can open the data in Excel and manipulate it as they normally do, but the code is done using the latest technology. So, there are a lot of possibilities. I hope this will be a useful outlet and all constructive criticism is welcome.
Recently, I ran into an issue where a customer wanted to display currencies in data bound textboxes, but there didn’t seem to be an easy way to do this. Luckily, I found the information in the MSDN forums and I figured I would create a simple project to bring together a bunch of threads on the subject. Here is the source code - Source Code .
So assuming you already have bound textboxes, here are three approaches I have found to deal with this issue.
1. Set the bindings through code In this case you set up the binding through code and now you need to modify it to display the data as a currency. Here is the code I used:
‘Formatting codes found at MSDN - Standard Numeric Format Strings http://msdn.microsoft.com/en-us/library/aa720653.aspx
Me.TextBox4.DataBindings.Add(“text”, PriceBindingSource, “Price”, True, DataSourceUpdateMode.OnPropertyChanged, ” “, “C”)
2. Format the field during an event I used the textchanged event, but how you do this is really based on the behavior you want.
Private Sub TextBox2_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox2.TextChangedMe.TextBox2.Text = FormatCurrency(Me.TextBox2.Text, 2)End Sub
3. Use Visual Studio Wizard Finally, you can use the Visual Studio IDE to set the bindings. What you need to do is select the textbox and expand the (DataBindings) property. Select the Advanced Binding field and then click on the button that appears in the field. A window will pop up and from there you can set the formatting for the textbox.

Hopefully this tidbit helps