Solved

Urgent Help needed-Converting Datatable Rows into Columns while binding to Infragistics grid

Posted on 2010-11-07
6
791 Views
Last Modified: 2013-11-15
Hi, Here is my scenario..
I am using Infragistics Ultra wingrid and binding with datatable

My data in SQL table

Date            Region            Percentage

1/1/2010        a              1.1

1/1/2010        b              2.2

1/1/2020        c              3.2

2/2/2010          a              2.5

2/2/2010          d              4.5



In Grid I have to show the data in this format..

Date               a            b      c      d    (Column headers in GRID)

1/1/2010       1.1      2.2    3.2    

2/2/2010       2.5                     4.5



Date and Region name shd be column header in the grid. please some one help me how to do this task..
0
Comment
Question by:gautam_reddyc
6 Comments
 
LVL 12

Expert Comment

by:rajapandian_81
ID: 34083054
I believe PIVOT operator in sql server can be used to achieve this kind of output. That result can be binded to grid.  Refer:
http://sqlserveradvisor.blogspot.com/2009/03/sql-server-convert-rows-to-columns.html

Sql Server experts can help you more on this.
0
 
LVL 3

Accepted Solution

by:
abdkhlaif earned 500 total points
ID: 34083134
you can do that using DataTable & GridView:
DataTable dtOriginal = new DataTable();

// ...

// Init your DataTable here

// ...



// Display the contents of the original table:

GridView1.DataSource = dtOriginal;

GridView1.DataBind();



// Create a new table for the summerized data:

DataTable dtSummary = new DataTable();

dtSummary.Columns.Add("Date"); // add column for the Date

foreach (DataRow dr in dtOriginal.Rows)

{

	if (!dtSummary.Columns.Contains(dr["Region"].ToString())) // make sure the column is not duplicated

		dtSummary.Columns.Add(dr["Region"].ToString()); // add the Region as a new column

}



foreach (DataRow dr in dtOriginal.Rows)

{

	string date = dr["Date"].ToString();

	foreach (DataRow drDate in dtOriginal.Select("Date = '" + date + "'"))

	{

		string region = dr["Region"].ToString();

		string percentage = dr["Percentage"].ToString();



		if (dtSummary.Select("Date = '" + date + "'").Length > 0) // if the date group already exists

		{

			dtSummary.Select("Date = '" + date + "'")[0][region] = percentage; // set the percentage under the given region

		}

		else // otherwise, add a new row:

		{

			nr = dtSummary.NewRow();

			nr["Date"] = date;

			nr[region] = percentage;

			dtSummary.Rows.Add(nr);

		}

	}

}



// Display the results:

GridView2.DataSource = dtSummary;

GridView2.DataBind();

Open in new window

0
 

Expert Comment

by:triquetrus
ID: 34084379
Given the above problem, I would have proceeded in the following order.

Step 1: Get the data from SQL and load in a local DataSetMain variable.
Step 2: Create a temporary DataSetTemp variable
Step 3: Get the unique set of regions and create the columns in DataSetTemp dataset table
Step 4: Get the unique set of dates
Step 5: For each date in the Step 4, Query the DataSetMain variable (using DataView.RowFilter property) and create a row in the DataSetTemp
Step 6: Bind the DataSetTemp to the grid.

Hope this helps, Sorry for not giving you a code snippet.
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

Author Comment

by:gautam_reddyc
ID: 34084675
Thanks for all for quick reply... I am not sure how to achieve this in SQL... I will try abdkhlaif solution and let you know... Hope this helps me ...

Thanks...
0
 

Author Comment

by:gautam_reddyc
ID: 34087121
Hi abdkhlaif,

I am facing litle problem with your code... Can u pls help me..
foreach (DataRow drDate in dtOriginal.Select("Date = '" + date + "'"))
      {
            string region = dr["Region"].ToString();
            string percentage = dr["Percentage"].ToString();

            if (dtSummary.Select("Date = '" + date + "'").Length > 0) // if the date group already exists
            {
                  dtSummary.Select("Date = '" + date + "'")[0][region] = percentage; // set the percentage under the given region
            }
            else // otherwise, add a new row:
            {

Its not entering the Inner ForEach loop... In your case dtOriginal has data but dtSummary is coming empty with just column names.. No data is copied to dtSummary..
Please help me what is gng wrong in the inner ForEach loop.

Thanks in advance...


0
 

Author Closing Comment

by:gautam_reddyc
ID: 34087557
Thanks abdkhlaif ... It worked....
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Loop not working 29 47
Get id from json Data with NewtonSoft 3 34
Anyway to make "All" the default in the dropdown? 6 34
Not showing page correctly 3 30
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now