Hi guys, new to site - and fairly new to C#. So be gently.

I have an xml file containing the following:

<?xml version="1.0" encoding="utf-8" ?>
<Arm>
    <Arm1> 
             <Name> Flies </Name>
             <Weight> 18 </Weight>
             <Reps> 12 </Reps>
    </Arm1> 
</Arm>

The xml contains more that just the one Arm child. It contains Arm2, Arm3 - up to 10 with different attributes, ie weight reps and names.

On the application side, i have the user to decide how many arm exercises he wants (from 1-5). I then use random generator to pick a random number. I have a string value that adds "Arm" plus the random number. It then uses a while loop as per the number of arm exercies the user wants.

This will then grab the values from the xml based on the Datamember, ie.. Arm6

I can do this, but my two problems are so:

1. It loops fine, but the datagridview row is being overwritten, rather than it adding a new row. How do i keep the existing line and add a new row?
2. I have predefined headers for the datagridview with formatting and colurs and such. Is it possible to only bring through the inner text rather than it pull through the header values as well?

I can supply the sample C# code if needed. Or i can explain further. I have seen much reference to using DataTables but i cannot get a working example.

Thanks, rooboy69

Recommended Answers

All 6 Replies

Hi rooboy69, welcome at Daniweb!
Some C# code would be nice. :)
Could not understand the second part of your question well, so please explain.

Hi ddanbe, thanks for the welcome, and the response.

My code is below. As mentioned in the original post, the user enters a number - this is set to UserV.

public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            int Count = 0;
            int UserV = 5;

            XmlDataDocument xmldatadoc = new XmlDataDocument();
            xmldatadoc.DataSet.ReadXml("C:\\test.xml");
            
                while (Count != UserV)
                {
                    Random Rand = new Random();
                    int ResultNumI = (int)(Rand.NextDouble() * 6) + 1;
                    string ResultNumT = "Arm" + ResultNumI;
                    Count = Count + 1;

                    dataGridView1.DataSource = xmldatadoc.DataSet;
                    dataGridView1.DataMember = ResultNumT;

                    if (Count == ResultNumI)
                        break;
                }
        }

So my aim is to bring through 5 lines in this instance, chosen randomly from the xml using the ResultNumT to pick one each time. Then have the five lines inserted into the dgv, or inserted one at a time in the loop.

Within the dgv, i gave it three column headers:
Exercise Weight Reps

What it appears to be doing is bring in the data as such:

Exercise Weight Reps Name Weight Reps
Flies 18 12

And leaving my original columns blank. As well as the first point - where it re-inserts over itself instead of adding new line.

Hope that helps clear it up mate.
thanks

Can you ZIP your project with the XML sample data and upload it? Click on "Go Advanced" (next to post quick reply) then click "Manage Attachments". One problem I see off-hand is:

dataGridView1.DataSource = xmldatadoc.DataSet;
                    dataGridView1.DataMember = ResultNumT;

That shouldn't be running inside of a loop

Hi,

I have attached a zip of the xml file. For you.

Thanks in advance

This one should include project as well. Didnt know what was needed so copied whole folder. Not big though.

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.