Data table results

Hi,

I have created a datatable that populatesa radgrid which works fine. The problem is the way the data is being displayed. I seem to be getting all the data for a column before the data for the second column get populated. so the grid looks like

JAN  FEB  MAR
1
2
3
4
5
         1
         2
         3
          4
          5
                 1
                 2
                 3
                 4
                 5


Instead of
JAN  FEB  MAR
1         1       1
2         2       2
3         3       3
4         4       4
5         5       5


I have attached my code .

Thanks in advance
DataTable dt = new DataTable();
            DataColumn newCol = new DataColumn();
            DataRow newRow = dtNewData.NewRow();
            
            

            for (int iC = 0; iC < table; iC++)
            {

                //add new columns
                string seriesname = XXXXXXX;
                
                newCol.ColumnName = seriesname;
                newCol.DataType = Type.GetType("System.String");
                dt.Columns.Add(newCol);
                newCol = null;
                newCol = new DataColumn();

            
                for (int iR = 0; iR < table; iR++)
                {

                    string value = XXXXXXX

                    newRow[dt.Columns[iC]] = value;
                    dt.Rows.Add(newRow);
                    newRow = dt.NewRow();
                }

            }
            return dt;

Open in new window

cjJosephjAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

robastaCommented:
The rows loop must be moved out of the columns loop

You want something like this:

0
robastaCommented:
protected void Page_Load(object sender, EventArgs e)
        {
            DataTable dt = new DataTable();
            DataColumn newCol = new DataColumn();

            int table = 3;

            for (int iC = 0; iC < table; iC++)
            {
                //add new columns
                string seriesname = "Column" + iC;

                newCol.ColumnName = seriesname;
                newCol.DataType = Type.GetType("System.String");
                dt.Columns.Add(newCol);
                newCol = null;
                newCol = new DataColumn();


            }
            for (int iR = 0; iR < table; iR++)
            {
                DataRow newRow = dt.NewRow();
                string value = iR.ToString();

                newRow[dt.Columns[0]] = "Column1 Row" + iR;
                newRow[dt.Columns[1]] = "Column2 Row" + iR;
                newRow[dt.Columns[2]] = "Column3 Row" + iR;
                dt.Rows.Add(newRow);
                newRow = dt.NewRow();
            }
            GridView1.DataSource = dt;
            GridView1.DataBind();
        }


0
cjJosephjAuthor Commented:
Hi,

Thanks for the quick response.

But i can not move the rows loop out of the columns loop as iC from the column loop is used in the row loop
           
                for (int iR = 0; iR < table; iR++)
                {
 
                    string value = XXXXXXX
 ****iC FROM THE FIRST LOOP**********
                    newRow[dt.Columns[iC]] = value;
                    dt.Rows.Add(newRow);
                    newRow = dt.NewRow();
                }

thanks
0
robastaCommented:
That would not work, because when IC is 0, we will only enter the values into Column 0.
Look at it this way, when you create the column Jan, you will only have one column. (i.e. you cannot add cells to column Feb which does not exist yet.) That is why you need to create the columns seperately, then the rows.


0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.