Sunday, 13 July 2014

C# ListView

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
namespace ListV
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
#region Working
DataTable table = new DataTable();
table.Columns.Add("MainGroup", typeof(string));
table.Columns.Add("Subject", typeof(string));
table.Rows.Add("Class A", "English");
table.Rows.Add("Class B", "Science");
table.Rows.Add("Class C", "Hindi");
table.Rows.Add("Class B", "Computers");
table.Rows.Add("Class C", "Maths");
//GridView1.DataSource = table;
//GridView1.DataBind();
#endregion
if (!IsPostBack)
{
try
{
DataTable dt = new DataTable();
dt = table;
DataTable dydt = new DataTable("test");
string test = string.Empty;
foreach (DataRow myRow in dt.Rows)
{
foreach (DataColumn myCol in dt.Columns)
{
if (myCol.ColumnName.ToString() == "MainGroup")
{
if (!dydt.Columns.Contains(myRow[myCol].ToString()))
{
dydt.Columns.Add(myRow[myCol].ToString(), typeof(System.String));
}
}
}
}
foreach (DataRow myRow in dt.Rows)
{
string strSubject = myRow["Subject"].ToString();
foreach (DataColumn myCol in dt.Columns)
{
if (myCol.ColumnName.ToString() == "MainGroup")
{
string strMainGroup = myRow[myCol].ToString();
foreach (DataColumn myfinalCol in dydt.Columns)
{
if (myfinalCol.ColumnName.ToString().Trim() == strMainGroup)
{
DataRow row;
row = dydt.NewRow();
row[strMainGroup] = strSubject;
dydt.Rows.Add(row);
}
}
}
}
}
GridView1.DataSource = dydt;
GridView1.DataBind();
}
catch (Exception ex)
{
lblError.Visible = true;
lblError.Text = "Error" + ex.StackTrace + "Message: " + ex.Message;
}
}
}
}
}
ListView