Introduction
In this article I will explain how to bind GridView using Array, List and ArrayList .If you are familiar with Arrays, List, Collections in asp.net then this article will be better understable for you.
Description
We don't need to any database ,all the data inserted from backend(.Cs) Page.
Aspx Page -- -- -- -- -- -- -- --
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:TemplateField HeaderText="Customer
Names">
<ItemTemplate>
<%# Container.DataItem %>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Using Array -- -- -- -- -- -- -- --
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
//Populate GridView using Array of string.
string[] objNames = new string[4];
objNames
[0] = "Jatin Phulera";
objNames
[1] = "Gaurav Pandey";
objNames
[2] = "Nicks Joshi";
objNames
[3] = "King Sam";
objNames [4] = "Shiv Deopa";
GridView1.DataSource = objNames;
GridView1.DataBind();
}
}
Using ArrayList -- -- -- -- -- -- -- --
Firstly add a namespace on top for using ArrayList
using System.Collections;
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
//Populate GridView using ArrayList.
ArrayList objArrayListNames
= new ArrayList();
objArrayListNames.Add("Jatin
Phulera");
objArrayListNames.Add("Gaurav
Pandey");
objArrayListNames.Add("Nicks
Joshi");
objArrayListNames.Add("King
Sam");
objArrayListNames.Add("Shiv
Deopa”);
GridView1.DataSource = objArrayListNames;
GridView1.DataBind();
}
}
Using List-- -- -- -- -- -- -- --
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
//Populate GridView using List of string.
List<string> objListNames = new List<string>();
objListNames.Add("Jatin
Phulera");
objListNames.Add("Gaurav
Pandey");
objListNames.Add("Nicks
Joshi");
objListNames.Add("King
Sam");
objListNames.Add("Shiv Deopa");
GridView1.DataSource = objListNames;
GridView1.DataBind();
}
}
0 comments:
Post a Comment