Here is the Table and I want to convert this rows into Colums.
Default.aspx:
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server"></asp:GridView>
</div>
</form>
</body>
</html>
.cs page Code.
protected void Page_Load(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(" ");
SqlCommand cmd = new SqlCommand("select * from country", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
DataTable Return = GetDataTable(dt, "CountryID");
GridView1.DataSource = Return;
GridView1.DataBind();
}
public static DataTable GetDataTable(DataTable table, string columnXAxis,
params string[] columnsToIgnore)
{
DataTable table1 = new DataTable();
if (columnXAxis == "")
columnXAxis = table.Columns[0].ColumnName;
table1.Columns.Add(columnXAxis);
List<string> columnXValues = new List<string>();
List<string> listColumnsToIgnore = new List<string>();
if (columnsToIgnore.Length > 0)
listColumnsToIgnore.AddRange(columnsToIgnore);
if (!listColumnsToIgnore.Contains(columnXAxis))
listColumnsToIgnore.Add(columnXAxis);
foreach (DataRow dr in table.Rows)
{
string columnXTemp = dr[columnXAxis].ToString();
if (!columnXValues.Contains(columnXTemp))
{
columnXValues.Add(columnXTemp);
table1.Columns.Add(columnXTemp);
}
else
{
throw new Exception("Insertion Failed" + columnXAxis);
}
}
foreach (DataColumn dc in table.Columns)
{
if (!columnXValues.Contains(dc.ColumnName) &&
!listColumnsToIgnore.Contains(dc.ColumnName))
{
DataRow dr = table1.NewRow();
dr[0] = dc.ColumnName;
table1.Rows.Add(dr);
}
}
for (int i = 0; i < table1.Rows.Count; i++)
{
for (int j = 1; j < table1.Columns.Count; j++)
{
table1.Rows[i][j] =
table.Rows[j - 1][table1.Rows[i][0].ToString()].ToString();
}
}
return table1;
}
Output:
Default.aspx:
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server"></asp:GridView>
</div>
</form>
</body>
</html>
.cs page Code.
protected void Page_Load(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(" ");
SqlCommand cmd = new SqlCommand("select * from country", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
DataTable Return = GetDataTable(dt, "CountryID");
GridView1.DataSource = Return;
GridView1.DataBind();
}
public static DataTable GetDataTable(DataTable table, string columnXAxis,
params string[] columnsToIgnore)
{
DataTable table1 = new DataTable();
if (columnXAxis == "")
columnXAxis = table.Columns[0].ColumnName;
table1.Columns.Add(columnXAxis);
List<string> columnXValues = new List<string>();
List<string> listColumnsToIgnore = new List<string>();
if (columnsToIgnore.Length > 0)
listColumnsToIgnore.AddRange(columnsToIgnore);
if (!listColumnsToIgnore.Contains(columnXAxis))
listColumnsToIgnore.Add(columnXAxis);
foreach (DataRow dr in table.Rows)
{
string columnXTemp = dr[columnXAxis].ToString();
if (!columnXValues.Contains(columnXTemp))
{
columnXValues.Add(columnXTemp);
table1.Columns.Add(columnXTemp);
}
else
{
throw new Exception("Insertion Failed" + columnXAxis);
}
}
foreach (DataColumn dc in table.Columns)
{
if (!columnXValues.Contains(dc.ColumnName) &&
!listColumnsToIgnore.Contains(dc.ColumnName))
{
DataRow dr = table1.NewRow();
dr[0] = dc.ColumnName;
table1.Rows.Add(dr);
}
}
for (int i = 0; i < table1.Rows.Count; i++)
{
for (int j = 1; j < table1.Columns.Count; j++)
{
table1.Rows[i][j] =
table.Rows[j - 1][table1.Rows[i][0].ToString()].ToString();
}
}
return table1;
}
Output: