How to convert gridview coulnm in rows using asp.net c#.

How to convert gridview coulnm in rows using asp.net c#.

I am going to explain how to convert grdiview colunm in rows asp.net c#.This is simple toimplement.I stored database records in datatable and after that i convert all datatable rows into columns and then convert column into rows.After complete this process then i just bind the gridview with converted datatable.I convert gridview column into rows on button click but you can use this as you required.

0Demo1

Run this script in sql server –

CREATE TABLE [dbo].[Articletable](

[ID] [int] NOT NULL,

[Title] [varchar](200) NULL,

[Description] [varchar](400) NULL,

[Author] [varchar](50) NULL

) ON [PRIMARY]

Method for convert column into rows is below-

public static DataTable ConvertGrid(DataTable dt)

{

DataTable dtConvert = new DataTable();

for (int i = 0; i <= dt.Rows.Count; i++)

{

dtConvert.Columns.Add(“Col_”+Convert.ToString(i));

}

for (int i = 0; i < dt.Columns.Count; i++)

{

dtConvert.Rows.Add();

dtConvert.Rows[i][0] = dt.Columns[i].ColumnName;

for (int j = 0; j < dt.Rows.Count; j++)

{

dtConvert.Rows[i][j + 1] = dt.Rows[j][i];

}

}

return dtConvert;

}

For use this above method in project first create a website in visual studio and then add a page and then design your page as shown below-

<%@ Page Language=”C#” AutoEventWireup=”true”CodeFile=”GridRowAsColumns.aspx.cs”

Inherits=”GridRowAsColumns” %>

<!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></title>

<link href=”ClassicGridView.css” rel=”stylesheet”type=”text/css” />

</head>

<body>

<form id=”form1″ runat=”server”>

<div style=”margin-left: 50px; margin-top: 40px;”>

<h3>

Convert rows to columns and columns to rows in GridView using c#</h3>

<asp:GridView ID=”GrdSimple” runat=”server” Width=”48%”Font-Names=”Trebuchet MS”>

<HeaderStyle CssClass=”HeaderStyle”HorizontalAlign=”Center” />

<RowStyle CssClass=”RowStyle” HorizontalAlign=”Center” />

<AlternatingRowStyle CssClass=”AltRowStyle”HorizontalAlign=”Center” />

</asp:GridView>

<br />

<asp:Button ID=”btnConvert” runat=”server” Text=”Convert Grid Rows into Columns”

OnClick=”btnConvert_Click” />

</div>

</form>

</body>

</html>

After complete design page then write the c# code in code behind page.As below-

using System;

using System.Web.UI.WebControls;

using System.Data;

using System.Configuration;

using System.Data.SqlClient;

 

public partial class GridRowAsColumns : System.Web.UI.Page

{

SqlConnection con = new SqlConnection();

protected void Page_Load(object sender, EventArgs e)

{

con.ConnectionString = ConfigurationManager.ConnectionStrings[“cn”].ConnectionString;

if (con.State == ConnectionState.Closed)

{

con.Open();

}

GrdSimple.DataSource = dt_Grid();

GrdSimple.DataBind();

}

 

private DataTable dt_Grid()

{

DataTable dt = new DataTable();

string qry = “select * from Articletable”;

SqlDataAdapter adp = new SqlDataAdapter(qry,con);

adp.Fill(dt);

return dt;

}

 

public static DataTable ConvertGrid(DataTable dt)

{

DataTable dtConvert = new DataTable();

for (int i = 0; i <= dt.Rows.Count; i++)

{

dtConvert.Columns.Add(“Col_”+Convert.ToString(i));

}

for (int i = 0; i < dt.Columns.Count; i++)

{

dtConvert.Rows.Add();

dtConvert.Rows[i][0] = dt.Columns[i].ColumnName;

for (int j = 0; j < dt.Rows.Count; j++)

{

dtConvert.Rows[i][j + 1] = dt.Rows[j][i];

}

}

return dtConvert;

}

 

protected void btnConvert_Click(object sender, EventArgs e)

{

GrdSimple.ShowHeader = false;

GrdSimple.DataSource = ConvertGrid(dt_Grid());

GrdSimple.DataBind();

foreach (GridViewRow GridRows in GrdSimple.Rows)

{

GridRows.Cells[0].CssClass = “HeaderStyle”;

GridRows.Cells[0].Font.Bold = true;

}

btnConvert.Text = “Successfully Converted”;

}

}

About the author

admin administrator

Leave a Reply