Convert Row to Column

Posted by Suneel161 under C# on 7/22/2013 | Points: 10 | Views : 3119 | Status : [Member] | Replies : 3
Hi,

How to convert row to column.
Actual Input:

1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16 17

Output:

4 8 12 17
3 7 11 16
15
2 6 10 14
1 5 9 13




Responses

Posted by: Prabhukiran345 on: 7/24/2013 [Member] Starter | Points: 25

Up
0
Down
Hi,

Use This Code. This may help you

public DataTable ConvertColumnsAsRows(DataTable dt)
{
DataTable dtnew=new DataTable();
//Convert all the rows to columns
for (int i = 0; i <= dt.Rows.Count; i++)
{
dtnew.Columns.Add(Convert.ToString(i));
}
DataRow dr;
// Convert All the Columns to Rows
for (int j = 0; j < dt.Columns.Count; j++)
{
dr = dtnew.NewRow();
dr[0] = dt.Columns[j].ToString();
for (int k = 1; k <= dt.Rows.Count; k++)
dr[k] = dt.Rows[k - 1][j];
dtnew.Rows.Add(dr);
}
return dtnew;
}

Mark as Answer if Helpful

Thanks,
Prabhu Kiran B

Thanks,
Prabhu Kiran Bommareddy

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

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

Up
0
Down
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="FlipGridView.aspx.cs" Inherits="XmlPostData" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" ShowHeader="false" AllowSorting="true">
</asp:GridView>
</div>
</form>
</body>
</html>



using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Net;
using System.Text;
using System.IO;

public partial class FlipGridView:System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

GridView1.DataSource = FlipDataSet(c());
GridView1.DataBind();

}

public DataSet c()
{
DataSet ds = new DataSet();
DataTable dt = new DataTable("Company");
DataRow dr;
dt.Columns.Add(new DataColumn("accountNo", typeof(Int32)));
dt.Columns.Add(new DataColumn("CompanyName", typeof(string)));
dt.Columns.Add(new DataColumn("Address", typeof(string)));
for (int i = 0; i <= 10; i++)
{
dr = dt.NewRow();
dr[0] = i;
dr[1] = "Company" + i + Environment.NewLine + "Title" + i;
dr[2] = "Address" + i + Environment.NewLine + "Title" + i;
dt.Rows.Add(dr);
}
ds.Tables.Add(dt);
return ds;
}
public DataSet FlipDataSet(DataSet my_DataSet)
{
DataSet ds = new DataSet();
foreach (DataTable dt in my_DataSet.Tables)
{
DataTable table = new DataTable();
for (int i = 0; i <= dt.Rows.Count; i++)
{
table.Columns.Add(Convert.ToString(i));
}
DataRow r = null;
for (int k = 0; k < dt.Columns.Count; k++)
{
r = table.NewRow();
r[0] = dt.Columns[k].ToString();
for (int j = 1; j <= dt.Rows.Count; j++)
r[j] = dt.Rows[j - 1][k];
table.Rows.Add(r);
}
ds.Tables.Add(table);
}

return ds;
}
}

http://www.codeproject.com/Articles/5030/Displaying-vertical-rows-in-a-DataGrid



Regards,
Jayakumar Selvakani

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

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

Up
0
Down
Refer this link
http://www.codeproject.com/Articles/44274/Transpose-a-DataTable-using-C

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

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

Login to post response