Let's begin by adding 2 GridViews to a page and a button. Now we need to create a Datatable. For simplicity, I am adding data to Datatable in Gridbind method and populating the Gridview. With that data manually you can fill Datatable with database. Store this Datatable in ViewState. Now we will call this method on page load.
For converting Gridview row to column I have created a method Transpose. Now we will do the following in transpose method.
- We need to create a new Datatable newdt. Retrieve the old Datatable from ViewState .The number of columns in new Datatable newdt will be equal to the number of rows in dt.
- Add columns to the new Datatable newdt (Column Name is passed as blank ).
- Create a datarow dr. Iterate through all columns and add for each column value get all the values of the rows in old Datatable dt and store it in dr.
- Add Datarow dr to newdt. Now add click event for the button and call the transpose method.
- < style type = "text/css" > .header {
- padding: 5px;
- background - color: #7ac0da; font-weight:bold;
- }
- .header content-wrapper table tr td {
- border:dotted 1px solid !important;
- }
- </style>
- <br />
- <div style= "text-align: center;" > < asp: Gridview alternatingrowstyle - backcolor = "#ccffff"
- bordercolor = "Gray"
- gridlines = "Both"
- headerstyle - backcolor = "#7ac0da"
- headerstyle - bordercolor = "Gray"
- headerstyle - cssclass = "header content-wrapper"
- id = "grdNormal"
- runat = "server" > < /asp:Gridview >
- </div > < div style = "text-align: center;" > < asp: linkbutton id = "lnkConvert"
- onclick = "lnkConvert_Click"
- runat = "server" > < img src = "Images/down.png"
- style = "width: 50px;" / > < /asp:linkbutton>
- </div > < div style = "text-align: center;" > < asp: Gridview bordercolor = "Gray"
- gridlines = "Both"
- headerstyle - bordercolor = "Gray"
- id = "grdTranspose"
- onrowcreated = "grdTranspose_RowCreated"
- runat = "server"
- showheader = "false" > < /asp:Gridview >
- </div > < /div>

Join the conversation! Your thoughts help the community grow.