I have two datatables. I want to display them in one Asp: GridView side by side.
Dt1 Col1 co2
Dt2 Col3 col4
I want GridView to display as Col1 col2 col3 col4
There is no relationship with those datatables.
I have two datatables. I want to display them in one Asp: GridView side by side.
Dt1 Col1 co2
Dt2 Col3 col4
I want GridView to display as Col1 col2 col3 col4
There is no relationship with those datatables.
You can use pivot table as follows this convert rows as columns
INSERT INTO #yourtable ([Id], [Value], [ColumnName])
VALUES
(1, 'John', 'FirstName'),
(2, '2.4', 'Amount'),
(3, 'ZH1E4A', 'PostalCode'),
(4, 'Fork', 'LastName'),
(5, '857685', 'AccountNumber');
SELECT
Firstname, Amount, PostalCode, LastName, AccountNumber
FROM
(SELECT
value, columnname
FROM
#yourtable) d
PIVOT
(MAX(value)
FOR columnname IN (Firstname, Amount, PostalCode, LastName, AccountNumber)
) piv;
My demo didn't check empty/null validation. If needed, please do.
//Create new class
public class FinalData
{
public string Col1 { get; set; }
public string Col2 { get; set; }
public string Col3 { get; set; }
public string Col4 { get; set; }
public FinalData(){}
}
//Populate your two tables into FinalData array as below
int MaxRows = dt1.Rows.Count > dt2.Rows.Count ? dt1.Rows.Count : dt2.Rows.Count;
FinalData[] fdList = new FinalData[MaxRows];
for (int i = 0; i < dt1.Rows.Count; i++)
{
FinalData[i] = new FinalData() { Col1 = dt1.Rows[i]["Col1"].ToString(), Col2 = dt1.Rows[i]["Col2"].ToString() };
}
for (int i = 0; i < dt2.Rows.Count; i++)
{
FinalData[i] = new FinalData() { Col3 = dt2.Rows[i]["Col3"].ToString(), Col4 = dt1.Rows[i]["Col4"].ToString() };
}
//Bind your gridview with fdList
YourGridview.DataSource = fdList;
YourGridView.DataBind();