How add duplicate column name into datatable ?

Posted by Cpatil1000 under ASP.NET on 8/19/2013 | Points: 10 | Views : 3454 | Status : [Member] | Replies : 2
I have write a store procedure and also passing some parameter. Now I am trying to making pivot table and binding to the gridview. But problem is that my datatable is not accepting duplicate column name. if my table column name is different. So how I can add same column name into datatable. First I am taking column from ROW_SOURCE of text name is ‘Sakhare Dam’ and second I am taking column from PURE_SOURCE of Textname is ‘Sakhare Dam’. So how do it take if taking different column name..
My coding getting error in second loop of for…
for (incVal = 0; incVal < source.Rows.Count; incVal++)
{
if (!report.Columns.Contains(source.Rows[incVal][(int)FieldsNRW.RAWSOURCE].ToString().ToUpper()))
{
report.Columns.Add(source.Rows[incVal][(int)FieldsNRW.RAWSOURCE].ToString().ToUpper(), typeof(decimal));
}
}

for (incVal = 0; incVal < source.Rows.Count; incVal++)
{
if (!report.Columns.Contains(source.Rows[incVal][(int)FieldsNRW.PURESOURCE].ToString().ToUpper()))
{
report.Columns.Add(source.Rows[incVal][(int)FieldsNRW.PURESOURCE].ToString().ToUpper(), typeof(decimal));
}
}

This is Table data…
ID DATE RAW_SOURCE RAW WATER PURE_SOURCE PURE WATER OUTGOING WATER MBR OUTGOING WATER ESR
47 1/8/2013 Sakhare Dam 5.94 Sakhare Dam 5.84 5.84 5.84
46 2/8/2013 Sakhare Dam 3.75 Sakhare Dam 3.65 3.65 3.65
45 3/8/2013 Sakhare Dam 5.3 Sakhare Dam 5.2 5.2 5.2
44 4/8/2013 Sakhare Dam 4.75 Sakhare Dam 4.65 4.65 4.65
43 5/8/2013 Sakhare Dam 4.84 Sakhare Dam 4.74 4.74 4.74
42 6/8/2013 Sakhare Dam 5.85 Sakhare Dam 5.75 5.75 5.75
41 7/8/2013 Sakhare Dam 6.21 Sakhare Dam 6.11 6.11 6.11
30 8/8/2013 Sakhare Dam 6.12 Sakhare Dam 6.02 6.02 6.02
31 9/8/2013 Sakhare Dam 4.48 Sakhare Dam 4.38 4.38 4.38
32 10/8/2013 Sakhare Dam 6.85 Sakhare Dam 6.75 6.75 6.75
33 11/8/2013 Sakhare Dam 6.67 Sakhare Dam 6.57 6.57 6.57
34 12/8/2013 Sakhare Dam 6.4 Sakhare Dam 6.3 6.3 6.3
35 13-08-2013 Sakhare Dam 6.03 Sakhare Dam 5.93 5.93 5.93
36 14-08-2013 Sakhare Dam 6.12 Sakhare Dam 6.02 6.02 6.02

I want output..

Date Sakhare Dam Sakhare Dam OUTGOING WATER MBR OUTGOING WATER ESR
1/8/2013 5.94 5.84 5.84 5.84




Responses

Posted by: Bandi on: 8/19/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
As per my knowledge you can't have duplicate column names in the Data table...
While retrieving data from database itself, append any Character or symbol to the column data... then bind to datatable....
Ex: If Row_source Column data is "Sakhare Dam", append it with "RS_"
SELECT 'RS_'+ ROW_SOURCE  AS ROW_SOURCE FROM TableName

Like this you can differ those two columns and bind data to datatable easily...
http://forums.codeguru.com/showthread.php?522419-Why-C-DataTable-can-have-duplicate-columns-from-stored-proc-but-not-hardcoded-duplic

Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Cpatil1000, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Ssj_Kumar on: 8/19/2013 [Member] Starter | Points: 25

Up
0
Down
You can easily implement this logic with sql Pivot, please find the below link for the sample
http://blog.sqlauthority.com/2008/06/07/sql-server-pivot-and-unpivot-table-examples/

Regards,
Jayakumar Selvakani

Cpatil1000, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response