Member Avatar

I'm hoping someone here can help me. I am very new to C# and programming in general and am stuck on the following after researching and trying different solutions for the past 2 weeks!

I'm trying to read from an XML file into datagridview. I also want to add extra data that a user inputs into 3 textboxes (an 'add data' button should update datagridview) I'd also like to be able to delete rows from datagridview. On exit, all data is saved into XML file. (I've attached the 'form' design)

The xml file is as follows:

<?xml version="1.0" encoding="utf-8"?>

I've tried so many different ways to do this that my code is a complete mess, and what I did have working is now broken :icon_cry:

From what I understand, I need to read the XML file into a dataset and then display that dataset in datagridview. I'm not sure where to go from there though.

Can anyone point me in the right direction here? Any tutorials that I can look at?

Thanks in advance

Recommended Answers

All 3 Replies

Member Avatar

Thanks! That certainly got me a bit closer. I have added a table to my (untyped) dataset, bound the dataset to the datagrid, however the xml file doesn't populate the grid. I'm sure I'm missing something here:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace LearningDataGridView
    public partial class LearningDataGridView : Form
        string sFile = "C:\\Orders.xml";

        public LearningDataGridView()

        private void btnQuit_Click(object sender, EventArgs e)

        private void LearningDataGridView_Load(object sender, EventArgs e)
            BindingSource bSource = new BindingSource();
            bSource.DataSource = dsOrders;
            dgOrders.DataSource = bSource;
            FileStream fsReadXml = new FileStream(sFile, FileMode.Open);
                this.dgOrders.DataSource = dsOrders.Tables[0].DefaultView;
            catch (Exception ex)

        private void btnAddData_Click(object sender, EventArgs e)
           // take user input from text boxes and add it to a new row in datagrid
            DataRow drNewRow = dsOrders.Tables[0].NewRow();
            drNewRow[0] = txtShareName.Text;
            drNewRow[1] = txtBuy.Text;
            drNewRow[2] = txtSell.Text;
            drNewRow[3] = txtNumberShares.Text;
            drNewRow[4] = txtTotal.Text;


            //clear text boxes
            txtShareName.Text = "";
            txtBuy.Text = "";
            txtSell.Text = "";
            txtNumberShares.Text = "";
            txtTotal.Text = "";

        private void btnWriteXML_Click(object sender, EventArgs e)
            // save all data in datagrid to XML file

        private void txtNumberShares_TextChanged(object sender, EventArgs e)
            if (txtNumberShares.Text != "")
                txtTotal.Text = Convert.ToString((Convert.ToDecimal(txtBuy.Text)) * (Convert.ToDecimal(txtNumberShares.Text)));


How about uploading your project so we can take a look at it?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.