Inner loop on datacolumns duplicates results - newbie .net question

C

Cameron Thomas

Hi There,

I am trying to loop throught the results of my query, my table has columns
that have colours and sizes with each colour have many sizes. Currently the
loop is just duplicating the colour size values for each colour. So for
instance if the colour has 2 of a particluar size the code writes that as
the value for all of the columns.

Couls someone help me out with the syntax please?

Cameron

desired result:

colour | size | size | size | size

colour | size | size | size | size



Dim dr As DataRow

Dim dc As DataColumn

Dim dc1 As DataColumn

For Each dr In ds.Tables(0).Rows

For Each dc In ds.Tables(0).Columns

If dc.ColumnName = "colour" Then

r = New TableRow

c = New TableCell

c.Controls.Add(New _

LiteralControl(dr("colour")))

c.VerticalAlign = VerticalAlign.Top

c.Style("background-color") = "lightblue"

r.Cells.Add(c)

For Each dc1 In ds.Tables(0).Columns

c = New TableCell

c.Controls.Add(New LiteralControl(dr("size")))

c.VerticalAlign = VerticalAlign.Top

r.Cells.Add(c)

Next

End If

Table2.Rows.Add(r)

Next

Next
 
G

Guest

Hi,
First of all, it looks like you are adding elements to a data grid (or
other control). You might want to add these things directly to the dataset.

Also, 'column' spans all rows (I think). If you set a column to a value,
I think all rows inherit that value. This is based on a possibly archaic
understanding of relational calculus and may not apply here.

I have included c# code here that I use to access all fields in a single row.

foreach (DataRow oDR in oldDataSet.Tables[d].Rows)
{
sDR = newDataSet.Tables[d].NewRow();
sDR.BeginEdit();
for(i=0; i < oDR.ItemArray.Length;i++)
{
try
{
sDR = (oDR.ItemArray.ToString()).Trim();
}
catch
{
}
}
sDR.EndEdit();

After I add the row to the dataset, the grid's databinding take care of
the rest.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top