Rikki
Rikki

Reputation: 646

dataGridView population from dataSet

I cannot get my dataGridView to update with all the tables in the dataSet. I just get one row in the dataGridView after I try to populate it.

I call this to fill my dataSet:

            DataTable table = new DataTable(DateTime.Now.ToString());

            table.Columns.Add("c1");
            table.Columns.Add("c2");
            table.Columns.Add("c3");
            table.Columns.Add("c4");
            table.Columns.Add("c5");
            table.Columns.Add("c6");
            table.Columns.Add("c7");
            table.Rows.Add("s1", "s2", "s3", "s4", "s5", "s6", "s7");

            dataSet1.Tables.Add(table);

            dataSet1.WriteXml("MyData.xml");

The data is written fine, but if I try to read it with this I only get one row populated, even if there's multiple entries in the dataSet.

        dataSet1.ReadXml("MyData.xml");
        MessageBox.Show((dataSet1.GetXml()));

I get the message with the data in the correct format, with the correct number of entries.

        dataGridView2.AutoGenerateColumns = true;

        int i2 = 0;
        while (i2 < dataSet1.Tables.Count)
        {
            dataGridView2.DataSource = dataSet1.Tables[i2];
            i2++;
        }

After this it will only show one row and not the amount of tables. The dataSet1.Tables.Count >= 2, but still only get 1 row.

Edit: It seems that each time I call this line:

dataGridView2.DataSource = dataSet1.Tables[i2];

It is deleting the previous row. Is there any way to append the row instead?

Upvotes: 0

Views: 515

Answers (2)

mccee
mccee

Reputation: 1667

You only created one table in the data set, so your while statement only executes once:

while (i2 < dataSet1.Tables.Count)
{
    ...
}

Count = 1 (number of tables) because you only called dataSet1.Tables.Add(table); one time.

dataSet1.WriteXml("MyData.xml") is only writing out the schema for that single table. So reading it back in will only read back in a single table's worth of schema.

Upvotes: 1

devilfish17
devilfish17

Reputation: 337

The following code appears to count the number of tables in the dataset:

while (i2 < dataSet1.Tables.Count)

Try pointing this to the table:

dataSet1.Tables["Yourtable"].Count

Upvotes: 0

Related Questions