Author : Prakash Pradeep Gopu
An array is a variable that holds multiple values of the same type.In arrays we have different types
1) Single dimensional array
An array is a variable that holds multiple values of the same type.In arrays we have different types
1) Single dimensional array
2) Two dimensional array
3) Multi dimensional array
Binding Single dimensional Array to the Grid view:
design your ASpx page with the following code
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>Grdview with Arraylist</title>
<style type="text/css">
.Gridview
{
font-family:Arial;
font-size:10pt;
font-weight:normal;
color:blue;
width:300px;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="gsinglevarray" runat="server" CssClass="Gridview" AutoGenerateColumns="false" HeaderStyle-BackColor="#7779AB" HeaderStyle-ForeColor="White" Caption="Single-Dimensional Array">
<Columns>
<asp:BoundField DataField="Name" HeaderText="Name"/>
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>
<head id="Head1" runat="server">
<title>Grdview with Arraylist</title>
<style type="text/css">
.Gridview
{
font-family:Arial;
font-size:10pt;
font-weight:normal;
color:blue;
width:300px;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="gsinglevarray" runat="server" CssClass="Gridview" AutoGenerateColumns="false" HeaderStyle-BackColor="#7779AB" HeaderStyle-ForeColor="White" Caption="Single-Dimensional Array">
<Columns>
<asp:BoundField DataField="Name" HeaderText="Name"/>
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>
In the code behind write the following code :
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindSingleDimenarray();
}
}
private void BindSingleDimenarray()
{
string[] Name = { "prakash", "pradeep", "Gopu", "India", "AndhraPradesh", "Warangal" };
DataTable dt = new DataTable();
dt.Columns.Add("Name");
for (int i = 0; i < Name.Count(); i++)
{
dt.Rows.Add();
dt.Rows[i]["Name"] = Name[i].ToString();
}
gsinglevarray.DataSource = dt;
gsinglevarray.DataBind();
}
{
if (!IsPostBack)
{
BindSingleDimenarray();
}
}
private void BindSingleDimenarray()
{
string[] Name = { "prakash", "pradeep", "Gopu", "India", "AndhraPradesh", "Warangal" };
DataTable dt = new DataTable();
dt.Columns.Add("Name");
for (int i = 0; i < Name.Count(); i++)
{
dt.Rows.Add();
dt.Rows[i]["Name"] = Name[i].ToString();
}
gsinglevarray.DataSource = dt;
gsinglevarray.DataBind();
}
Now Save the application and run it you will get the following Output :
A Two Dimensional Array Binding :
Add the following code to same Aspx page:
<div>
<asp:GridView ID="gvarray" runat="server" CssClass="Gridview" AutoGenerateColumns="false" HeaderStyle-BackColor="#7779AF" HeaderStyle-ForeColor="White" Caption="Two-Dimensional Array">
<Columns>
<asp:BoundField DataField="Name" HeaderText="Name"/>
<asp:BoundField DataField ="Education" HeaderText="Education" />
</Columns>
</asp:GridView>
</div>
<asp:GridView ID="gvarray" runat="server" CssClass="Gridview" AutoGenerateColumns="false" HeaderStyle-BackColor="#7779AF" HeaderStyle-ForeColor="White" Caption="Two-Dimensional Array">
<Columns>
<asp:BoundField DataField="Name" HeaderText="Name"/>
<asp:BoundField DataField ="Education" HeaderText="Education" />
</Columns>
</asp:GridView>
</div>
Add New Method in the code Bihind as follows :
private void BindTwoDGridview()
{
string[,] NameList = {
{"Prakash", "B.Tech"},
{"Pradeep","MCA"},
{"Gopu","MBA"},
{"Harsha","B.Tech"}
};
DataTable dt = new DataTable();
dt.Columns.Add("Name");
dt.Columns.Add("Education");
for (int i = 0; i < NameList.GetLength(0); i++)
{
dt.Rows.Add();
dt.Rows[i]["Name"] = NameList[i, 0].ToString();
dt.Rows[i]["Education"] = NameList[i, 1].ToString();
}
gvarray.DataSource = dt;
gvarray.DataBind();
}
{
string[,] NameList = {
{"Prakash", "B.Tech"},
{"Pradeep","MCA"},
{"Gopu","MBA"},
{"Harsha","B.Tech"}
};
DataTable dt = new DataTable();
dt.Columns.Add("Name");
dt.Columns.Add("Education");
for (int i = 0; i < NameList.GetLength(0); i++)
{
dt.Rows.Add();
dt.Rows[i]["Name"] = NameList[i, 0].ToString();
dt.Rows[i]["Education"] = NameList[i, 1].ToString();
}
gvarray.DataSource = dt;
gvarray.DataBind();
}
Multi dimensional array :
Place the following code in the Same Aspx page :
<div>
<asp:GridView ID="GridViewMulti" runat="server" CssClass="Gridview" AutoGenerateColumns="false" HeaderStyle-BackColor="#7779AF" HeaderStyle-ForeColor="White" Caption="Multi-Dimensional Array">
<Columns>
<asp:BoundField DataField="Name" HeaderText="Name"/>
<asp:BoundField DataField ="Education" HeaderText="Education" />
<asp:BoundField DataField="Age" HeaderText="Age" />
<asp:BoundField DataField="Designation" HeaderText="Designation" />
</Columns>
</asp:GridView>
</div>
Create a New method in the code behind file and call this method in the Page_Load()
private void BindMultiGridview()
{
string[,] NameList = {
{"Prakash", "B.Tech","24","Software Engineer"},
{"Pradeep","MCA","25","Team Leader"},
{"Mahesh","MBA","22","Marketing Executive"},
{"Harsha","B.Tech","21","Tester"}
};
DataTable dt = new DataTable();
dt.Columns.Add("Name");
dt.Columns.Add("Education");
dt.Columns.Add("Age");
dt.Columns.Add("Designation");
for (int i = 0; i < NameList.GetLength(0); i++)
{
dt.Rows.Add();
dt.Rows[i]["Name"] = NameList[i, 0].ToString();
dt.Rows[i]["Education"] = NameList[i, 1].ToString();
dt.Rows[i]["Age"] = NameList[i, 2].ToString();
dt.Rows[i]["Designation"] = NameList[i, 3].ToString();
}
GridViewMulti.DataSource = dt;
GridViewMulti.DataBind();
}
Save the application and run it You will see the following out put :
Place the following code in the Same Aspx page :
<div>
<asp:GridView ID="GridViewMulti" runat="server" CssClass="Gridview" AutoGenerateColumns="false" HeaderStyle-BackColor="#7779AF" HeaderStyle-ForeColor="White" Caption="Multi-Dimensional Array">
<Columns>
<asp:BoundField DataField="Name" HeaderText="Name"/>
<asp:BoundField DataField ="Education" HeaderText="Education" />
<asp:BoundField DataField="Age" HeaderText="Age" />
<asp:BoundField DataField="Designation" HeaderText="Designation" />
</Columns>
</asp:GridView>
</div>
Create a New method in the code behind file and call this method in the Page_Load()
private void BindMultiGridview()
{
string[,] NameList = {
{"Prakash", "B.Tech","24","Software Engineer"},
{"Pradeep","MCA","25","Team Leader"},
{"Mahesh","MBA","22","Marketing Executive"},
{"Harsha","B.Tech","21","Tester"}
};
DataTable dt = new DataTable();
dt.Columns.Add("Name");
dt.Columns.Add("Education");
dt.Columns.Add("Age");
dt.Columns.Add("Designation");
for (int i = 0; i < NameList.GetLength(0); i++)
{
dt.Rows.Add();
dt.Rows[i]["Name"] = NameList[i, 0].ToString();
dt.Rows[i]["Education"] = NameList[i, 1].ToString();
dt.Rows[i]["Age"] = NameList[i, 2].ToString();
dt.Rows[i]["Designation"] = NameList[i, 3].ToString();
}
GridViewMulti.DataSource = dt;
GridViewMulti.DataBind();
}
Save the application and run it You will see the following out put :
No comments:
Post a Comment