Suppose , you have data stored in a array of structure.
let us say,
public struct myData
{
string name;
int age;
string city;
public myData(string pName,int pAge,string pCity)
{
name=pName;
age=pAge;
city=pCity;
}
}
...............
public void dataTableData()
{
List<myData> items=new List<myData>();
...add name,age,city of each person to the object 'data'.
//Now you need to add data present in the object 'items' to the GridView
DataTable table=new DataTable();
DataRow row=null;
//Create three columns 'Name','Age','City'
table.Columns.Add(new DataColumn("Name",typeOf(string));
table.Columns.Add(new DataColumn("Age",typeOf(int));
table.Columns.Add(new DataColumn("City",typeOf(string));
//Now loop through the items present in the object 'items'
foreach(myData item in items)
{
row=table.NewRow();
row["Name"]=item.name;
row["Age"]=item.age;
row["City"]=item.city;
table.Rows.Add(row);
}
//'DataView1' is 'Name' of the DataGridView. see the above attached snapshot
DataView1.DataSource=table;
}
let us say,
public struct myData
{
string name;
int age;
string city;
public myData(string pName,int pAge,string pCity)
{
name=pName;
age=pAge;
city=pCity;
}
}
...............
public void dataTableData()
{
List<myData> items=new List<myData>();
...add name,age,city of each person to the object 'data'.
//Now you need to add data present in the object 'items' to the GridView
DataTable table=new DataTable();
DataRow row=null;
//Create three columns 'Name','Age','City'
table.Columns.Add(new DataColumn("Name",typeOf(string));
table.Columns.Add(new DataColumn("Age",typeOf(int));
table.Columns.Add(new DataColumn("City",typeOf(string));
//Now loop through the items present in the object 'items'
foreach(myData item in items)
{
row=table.NewRow();
row["Name"]=item.name;
row["Age"]=item.age;
row["City"]=item.city;
table.Rows.Add(row);
}
//'DataView1' is 'Name' of the DataGridView. see the above attached snapshot
DataView1.DataSource=table;
}
Comments