Live Chat Icon For mobile
Live Chat Icon

How to show data vertically in a datagrid for a particular row

Platform: ASP.NET| Category: DataGrid

<asp:DataGrid  id='DataGrid1' runat='server'></asp:DataGrid>

VB.NET


Dim cn As SqlConnection
Dim da As SqlDataAdapter
Dim ds As DataSet

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
	’ Put user code to initialize the page here
	cn = New SqlConnection('server=localhost;uid=sa;pwd=;database=northwind')
	da = New SqlDataAdapter('Select * from orders where orderid=10248', cn) 
	ds = New DataSet()
	da.Fill(ds, 'Orders')
	DataGrid1.DataSource = GoDoReShape(ds)
	DataGrid1.DataBind()
End Sub ’Page_Load

Public Function GoDoReShape(ByVal ds As DataSet) As DataSet
	Dim NewDs As New DataSet()

	NewDs.Tables.Add()
	’Create Two Columns with names 'ColumnName' and 'Value'
	’ColumnName -> Displays all ColumnNames
	’Value -> Displays ColumnData
	NewDs.Tables(0).Columns.Add('ColumnName')
	NewDs.Tables(0).Columns.Add('Value')

	Dim dr As DataRow
	For Each dr In ds.Tables(0).Rows
		Dim dcol As System.Data.DataColumn
	                For Each dcol In ds.Tables(0).Columns
		                ’Declare Array
		                Dim MyArray As String() = {dcol.ColumnName.ToString(), dr(dcol.ColumnName.ToString()).ToString()}
		                NewDs.Tables(0).Rows.Add(MyArray)
		Next 
	Next 
	Return NewDs
End Function ’GoDoReShape

C#


SqlConnection cn ;
SqlDataAdapter da;
DataSet ds;

private void Page_Load(object sender, System.EventArgs e)
{
	// Put user code to initialize the page here
	cn =new SqlConnection('server=localhost;uid=sa;pwd=;database=northwind');
	da=new SqlDataAdapter ( 'Select * from orders where orderid=10248',cn);
	ds = new DataSet();
	da.Fill (ds,'Orders');
	DataGrid1.DataSource =GoDoReShape (ds);
	DataGrid1.DataBind (); 
}

public DataSet GoDoReShape(DataSet ds)
{
	DataSet NewDs=new DataSet();

	NewDs.Tables.Add();
	//Create Two Columns with names 'ColumnName' and 'Value'
	//ColumnName -> Displays all ColumnNames
	//Value -> Displays ColumnData
	NewDs.Tables[0].Columns.Add('ColumnName');
	NewDs.Tables[0].Columns.Add('Value');
		
	foreach(DataRow dr in ds.Tables [0].Rows )
	{
		foreach(System.Data.DataColumn dcol in ds.Tables[0].Columns)
		{            
			//Declare Array
			string[] MyArray={dcol.ColumnName.ToString(),dr[dcol.ColumnName.ToString()].ToString()};
			NewDs.Tables[0].Rows.Add(MyArray);
		}
	}
	return NewDs;								
}

Share with

Share on twitter
Share on facebook
Share on linkedin